473,769 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RegEx to find a word not enclosed in paranthesis

I have a text and I need to find a Word that are not enclosed in
paranthesis. Can it be done with a regex? Is someone could help me?
I am not familar with regex...

Example looking for WORD:
(there is a WORD in ( my string WORD )) and * WORD * to (find WORD)
and * WORD *

Should give me the to word between star (star ar not part of string)

thanks a lot

Oct 31 '06 #1
1 4215
I don't believe this can be done using Regular Expressions, at least not
practically. I'll tell you why:

In order to identify the WORD you're looking for, the only rule that can be
applied is that it is preceded by the exact same number of left and right
parentheses. That means that the number of left parentheses before the WORD
and the number of right parentheses before the WORD must be the same,
whether 0 or more, but the exact same number of each.

In addition, the left and right parentheses have to be in order, that is, if
there are 2 left parentheses, they must be followed (at some point) by 2
right parentheses. In other words, you can't have 1 left parenthesis
followed by 2 right parentheses followed by one left parenthesis. And you
can't start with right parentheses. You must always have a number higher
than 0 of left parenthesis, followed by some sequence of 0 or more
characters that is NOT "WORD" followed by the exact same number of right
parentheses.

Since Regular Expressions does not have the capacity to count, this can't be
done using Regular Expressions. However, as I was able to determine the rule
for identifying WORD, I also have some idea of how it might be done using
string and character manipulation.

Since you're looking for the incidences of a string within a string, you
don't need to actually match the string, but only to know what the indices
of the incidences of the string within the origin string are. That is, once
you know the indices of the incidences, and you know what the search string
is, you can find them all within the string any time you need to.

You would need 2 variables, one to keep a count of left parenteses, and one
to keep a count of right parentheses. When you hit a left parenthesis,
increment the left parenthesis variable. If the 2 variables are not of equal
value, you don't do anything. If they are, you begin to check the characters
following for the search string ("WORD"). Here's an example. I've tested
this using all possible combinations, with one exception. It assumes that
left and right parentheses will always be in left-right order. That is, if
there is a stray parenthesis, or if the parentheses are somehow reversed in
the string, it may not work as advertised, and you may need to revise it:

/// <summary">
/// Finds the indices of all incidences of <paramref name="searchStr ing"/>
/// found in <paramref name="origin"/that are not
/// enclosed within parentheses.
/// </summary>
/// <param name="origin">S tring to Search.</param>
/// <param name="searchStr ing">String to Find.</param>
/// <returns>An array of the indices of all incidences of <paramref
name="searchStr ing"/>
/// found in <paramref name="origin"/that are not enclosed within
parentheses,
/// or an empty integer array if not found.</returns>
public static int[] IndicesWithoutP arentheses(stri ng origin, string
searchString)
{
char c;
int i, count = 0;
int leftCount = 0, rightCount = 0;
int originIndex, searchIndex;

int originLength = origin.Length;
int searchLength = searchString.Le ngth;

int[] indices = new int[originLength]; // holds indices found
int[] result; // return value
for (i = 0; i < indices.Length; i++)
indices[i] = -1; // No index

// Iterate through the origin string
for (originIndex = 0; originIndex < originLength; originIndex++)
{
c = origin[originIndex]; // Current char
if (c == '(') leftCount++; // Count left parentheses
else if (c == ')') rightCount++; // Count right parentheses
else if (leftCount == rightCount)
{
i = originIndex;
// Find the first letter of searchString prior to any left parenthesis
while (i < origin.Length && origin[i] != searchString[0] &&
origin[i] != '(') i++;
// if we've reached the end of the origin string, we're done.
if (i == origin.Length) break;
// Otherwise, we set originIndex to i, and begin searching for
searchString
originIndex = i + 1;
if (origin[i] == '(')
{
leftCount++;
originIndex--;
continue;
}
// Begin looking for searchString
for (searchIndex = 1; searchIndex < searchLength; i++)
if (searchString[searchIndex++] != origin[originIndex++]) break;
// if the loop did not break, we have found one
if (searchIndex == searchLength) indices[count++] = originIndex -
searchIndex;
originIndex--; // need to back up one because outer loop increments.
}
}
i = Array.IndexOf<i nt>(indices, -1);
if (i <= 0) result = new int[0];
else
{
result = new int[i];
Array.Copy(indi ces, result, i);
}
return result;
}

--
HTH,

Kevin Spencer
Microsoft MVP
Short Order Coder
http://unclechutney.blogspot.com

The devil is in the yada yada yada
<vm*****@gmail. comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
>I have a text and I need to find a Word that are not enclosed in
paranthesis. Can it be done with a regex? Is someone could help me?
I am not familar with regex...

Example looking for WORD:
(there is a WORD in ( my string WORD )) and * WORD * to (find WORD)
and * WORD *

Should give me the to word between star (star ar not part of string)

thanks a lot

Oct 31 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3384
by: Mark | last post by:
Hi, I've seen some postings on this but not exactly relating to this posting. I'm reading in a large mail message as a string. In the string is an xml attachment that I need to parse out and remove from the message once processed. I have to do this as a string and not using any CDO libraries. My problem is that there's normally a large pdf in the file so when I read the file in it's massive and I don't knwo if the XML is at the...
7
2618
by: bill tie | last post by:
I'd appreciate it if you could advise. 1. How do I replace "\" (backslash) with anything? 2. Suppose I want to replace (a) every occurrence of characters "a", "b", "c", "d" with "x", (b) every occurrence of characters "p", "q", "r", "s" with "y". Right now, I do it as follows:
0
1048
by: Derrick | last post by:
Hi Dave - Thanks, I'll give an easier example, say I have: sodium ion test and I search for "sodium ion test", matching, tagging, I end up with, say TEST${sodium ion test}
17
3979
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher http://forta.com/books/0672325667/
11
3111
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend a hand? import regsub
2
2370
by: Alex Maghen | last post by:
This is a bit of an abuse of this group. Just a nit, but I'm hoping someone really good with Regular Expressions can help me out here. I need to write a regular expression that will do the following: Search a whole blob of text (including newlines and everything). Find any text enclosed in brackets (), and replace it with a string I provide and then concatenate the text that had been enclosed in the brakets, without the brakets.
7
2588
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward slash then some numbers. For some reason I am not getting that. It won't work at all in 2.0
4
967
by: vmoreau | last post by:
I have a text and I need to find a Word that are not enclosed in paranthesis. Can it be done with a regex? Is someone could help me? I am not familar with regex... Example looking for WORD: (there is a WORD in ( my string WORD )) and * WORD * to (find WORD) and * WORD * Should give me the to word between star (star ar not part of string)
1
12208
by: jonnyboy6969 | last post by:
Hi All Really hoping someone can help me out here with my deficient regex skills :) I have a function which takes a string of HTML and replaces a term (word or phrase) with a link. The pupose is that I seek out terms which are in a glossary on our site, and automatically link to this definition. Its slightly complex becase certain elements have to be ignored, for exampleI dont want to add links within existing links, or for example link...
0
9586
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10043
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8869
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.