467,895 Members | 1,467 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,895 developers. It's quick & easy.

Need a regex for this...

I'm trying to learn regex but since I've spent way too much time on the
following "simple" case, there's obviously something I'm missing. I need to
find all occurrences of a specific non-whitespace character sequence in a
string, but only if it occurs somewhere within a set of parentheses and only
if it's a whole word. Assuming the sequence to be found is the word "the"
and the string to be searched is as follows, the only matches should be in
the last 4 cases (where whitespace may or may not occur after any opening
parenthesis and/or before any closing parenthesis:

the these (there) (the) (now is the) (the time) (now is the time) etc.

As you can see, if the whole word "the" is part of a larger string within
parentheses, it will have whitespace on one or both sides of it. But if it
is the only word, it may or may not. I know I can search for the whole word
anywhere in the string by using the regex \bthe\b but it's the parentheses
that are causing me the problems. I've tried all kinds of combinations that
I won't bore you with but obviously none have been completely correct.

And two more things... I've got both "RegexBuddy" and "Rad Software Regular
Expression Designer 1.4", which I've been using to help me wade through all
this. Do any of you have any other tools you would recommend for this
purpose, or a preference between these two? Also, does anyone know of a tool
that would allow me to state what I would like to achieve as an English
sentence, and would then produce a regex for it? (I know, keep dreaming! :-))

Thanks
Ray

Oct 14 '07 #1
  • viewed: 1776
Share:
3 Replies


Ray Mitchell wrote:
I'm trying to learn regex but since I've spent way too much time on the
following "simple" case, there's obviously something I'm missing. I need to
find all occurrences of a specific non-whitespace character sequence in a
string, but only if it occurs somewhere within a set of parentheses and only
if it's a whole word. Assuming the sequence to be found is the word "the"
and the string to be searched is as follows, the only matches should be in
the last 4 cases (where whitespace may or may not occur after any opening
parenthesis and/or before any closing parenthesis:

the these (there) (the) (now is the) (the time) (now is the time) etc.

As you can see, if the whole word "the" is part of a larger string within
parentheses, it will have whitespace on one or both sides of it. But if it
is the only word, it may or may not. I know I can search for the whole word
anywhere in the string by using the regex \bthe\b but it's the parentheses
that are causing me the problems. I've tried all kinds of combinations that
I won't bore you with but obviously none have been completely correct.
Did you try this:
\(([^)]*\bthe\b.*?\)
And two more things... I've got both "RegexBuddy" and "Rad Software Regular
Expression Designer 1.4", which I've been using to help me wade through all
this. Do any of you have any other tools you would recommend for this
purpose, or a preference between these two? Also, does anyone know of a tool
I'm using RegexTester, which can be downloaded from:
http://www.unibetter.com/deerchao/do...egexTester.zip
It's very light weight, and uses .Net Regex Engieen, and that's all
the reason why I use it.
that would allow me to state what I would like to achieve as an English
sentence, and would then produce a regex for it? (I know, keep dreaming! :-))

Thanks
Ray
So I guess you do know that's a dream :)
I think RegexBuddy is one of the closest tools to that dream.

Oct 14 '07 #2


"deerchao" wrote:
>

Ray Mitchell wrote:
I'm trying to learn regex but since I've spent way too much time on the
following "simple" case, there's obviously something I'm missing. I need to
find all occurrences of a specific non-whitespace character sequence in a
string, but only if it occurs somewhere within a set of parentheses and only
if it's a whole word. Assuming the sequence to be found is the word "the"
and the string to be searched is as follows, the only matches should be in
the last 4 cases (where whitespace may or may not occur after any opening
parenthesis and/or before any closing parenthesis:

the these (there) (the) (now is the) (the time) (now is the time) etc.

As you can see, if the whole word "the" is part of a larger string within
parentheses, it will have whitespace on one or both sides of it. But if it
is the only word, it may or may not. I know I can search for the whole word
anywhere in the string by using the regex \bthe\b but it's the parentheses
that are causing me the problems. I've tried all kinds of combinations that
I won't bore you with but obviously none have been completely correct.

Did you try this:
\(([^)]*\bthe\b.*?\)
And two more things... I've got both "RegexBuddy" and "Rad Software Regular
Expression Designer 1.4", which I've been using to help me wade through all
this. Do any of you have any other tools you would recommend for this
purpose, or a preference between these two? Also, does anyone know of a tool

I'm using RegexTester, which can be downloaded from:
http://www.unibetter.com/deerchao/do...egexTester.zip
It's very light weight, and uses .Net Regex Engieen, and that's all
the reason why I use it.
that would allow me to state what I would like to achieve as an English
sentence, and would then produce a regex for it? (I know, keep dreaming! :-))

Thanks
Ray

So I guess you do know that's a dream :)
I think RegexBuddy is one of the closest tools to that dream.

deerchao,

Yup, that's it. It was the lack of the question mark that was my problem.
Obviously I need to learn a little bit more about "greedy" v.s. "stingy", and
to think of how much time I wasted on this! Thanks for your help!

Ray
Oct 14 '07 #3
On Oct 14, 12:43 am, Ray Mitchell
<RayMitchell_NOSP...@MeanOldTeacher.comwrote:
And two more things... I've got both "RegexBuddy" and "Rad Software Regular
Expression Designer 1.4", which I've been using to help me wade through all
this. Do any of you have any other tools you would recommend for this
purpose, or a preference between these two?
I love Expresso.

Oct 14 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by jeevankodali | last post: by
2 posts views Thread by Michael R. Pierotti | last post: by
1 post views Thread by hillcountry74 | last post: by
9 posts views Thread by jmchadha | last post: by
6 posts views Thread by Extremest | last post: by
7 posts views Thread by Extremest | last post: by
15 posts views Thread by morleyc | last post: by
reply views Thread by Karch | last post: by
4 posts views Thread by Danny Ni | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.