471,319 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

regular expression to match substring xxx and not substring yyy

Hi,

Any idea about how to write a regular expression that matches a
substring xxx as long as the string does NOT contain substring yyy?

Thanks.

Kong

Dec 8 '05 #1
6 8905
<li****@email.com> wrote:
Any idea about how to write a regular expression that matches a
substring xxx as long as the string does NOT contain substring yyy?


My first thoughts would be:

1) Do you definitely need to use regular expressions? Are you matching
actual *patterns*, or just straight substrings?

2) If you *do* definitely need regular expressions, do you definitely
need to use a single one rather than checking it in two passes?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 8 '05 #2
li****@email.com wrote:
Hi,

Any idea about how to write a regular expression that matches a
substring xxx as long as the string does NOT contain substring yyy?

Thanks.

Kong


What do you want, and what do you not want to match? As Jon said, if
you need regular expressions, we can help you, but if you just need to
match a particlar string of letters, there are more readable ways to do it.
Dec 9 '05 #3

Jon,

1. I do need to match pattern (and I just used substring as a simple
example).
2. I am hoping to have a one pass (due to limitations of an existing
software).

The idea situation is to match pattern (i.e., match pattern1 but not
match pattern2) in one regular expression. The matched values of these
two patterns do not overlap, if this makes life easier.

I have struggled with lookahead, lookbehind, greedy/non-greedy, etc. in
RE without any luck.

Any pointers would be appreciated.

Thanks.

Kong

Jon wrote:
<li****@email.com> wrote:
Any idea about how to write a regular expression that matches a
substring xxx as long as the string does NOT contain substring yyy?


My first thoughts would be:

1) Do you definitely need to use regular expressions? Are you matching
actual *patterns*, or just straight substrings?

2) If you *do* definitely need regular expressions, do you definitely
need to use a single one rather than checking it in two passes?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Dec 9 '05 #4
<li****@email.com> wrote:
1. I do need to match pattern (and I just used substring as a simple
example).
2. I am hoping to have a one pass (due to limitations of an existing
software).

The idea situation is to match pattern (i.e., match pattern1 but not
match pattern2) in one regular expression. The matched values of these
two patterns do not overlap, if this makes life easier.

I have struggled with lookahead, lookbehind, greedy/non-greedy, etc. in
RE without any luck.

Any pointers would be appreciated.


Hmm... I'll have a little look when I get some time. I'm afraid I'm not
an expert on these matters...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 9 '05 #5
On 8 Dec 2005 14:21:10 -0800, li****@email.com wrote:
Hi,

Any idea about how to write a regular expression that matches a
substring xxx as long as the string does NOT contain substring yyy?

Thanks.

Kong


You posted elsewhere in this thread that xxx and yyy doesn't overlap.
That simplifies the problem. By using negative assertions it is
possible with the following expression:

(?<! .*yyy.*) #Don't match if yyy exists before xxx
xxx
(?! .*yyy.*) #Don't match if yyy exists after xxx

Note: RegexOptions.IgnorePatternWhitespace is used for readability

--
Marcus Andrén
Dec 9 '05 #6
It seems to work. Thanks!

Kong

Marcus Andrén wrote:
On 8 Dec 2005 14:21:10 -0800, li****@email.com wrote:
Hi,

Any idea about how to write a regular expression that matches a
substring xxx as long as the string does NOT contain substring yyy?

Thanks.

Kong


You posted elsewhere in this thread that xxx and yyy doesn't overlap.
That simplifies the problem. By using negative assertions it is
possible with the following expression:

(?<! .*yyy.*) #Don't match if yyy exists before xxx
xxx
(?! .*yyy.*) #Don't match if yyy exists after xxx

Note: RegexOptions.IgnorePatternWhitespace is used for readability

--
Marcus Andrén


Dec 9 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Holden Caulfield | last post: by
2 posts views Thread by Jonas | last post: by
10 posts views Thread by Lee Kuhn | last post: by
8 posts views Thread by Frank | last post: by
1 post views Thread by ENIZIN | last post: by
7 posts views Thread by Kevin CH | last post: by
25 posts views Thread by Mike | last post: by
reply views Thread by rosydwin | last post: by

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.