regular expression to match substring xxx and not substring yyy

  • Thread starter Thread starter likong
  • Start date Start date
L

likong

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
 
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?
 
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.
 
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
 
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...
 
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
 
It seems to work. 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
 
Back
Top