help for regular expression syntax

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I cannot seem to formulate what seems should be an easy expression. I need to
find all occurances of a pattern except when it contains a specific string.
For example if the string is

Dim s As String = "xmlns:b=""base"" xmlns:h=""http://www.mydomain.com""
xmlns:t=""type"""

I need to find both xmlns:b="base" and xmlns:t="type" and replace them, but
not replace the xmlns:h="http://www.mydomain.com" occurance.

using

Dim s As String = "xmlns:b=""base"" xmlns:h=""http://www.mydomain.com""
xmlns:t=""type"""
s = Regex.Replace(s, "\s*xmlns(:[^=]*)=""[^=]*""", "")

I can find an replace all, but how do I keep it from replacing
xmlns:h="http://www.mydomain.com"

thx in adv for help
 
jhgonzales said:
I cannot seem to formulate what seems should be an easy expression. I need to
find all occurances of a pattern except when it contains a specific string.
For example if the string is

Dim s As String = "xmlns:b=""base"" xmlns:h=""http://www.mydomain.com""
xmlns:t=""type"""

I need to find both xmlns:b="base" and xmlns:t="type" and replace them, but
not replace the xmlns:h="http://www.mydomain.com" occurance.

One approach is to use a "Zero-width negative lookahead assertion,"
like this:

\s* (?!xmlns:h="http://www.mydomain.com") xmlns(:[^=]*)="[^=]*"

RegexOptions.IgnorePatternWhitespace

This will get complicated, if you have a lot of patterns to ignore,
but not horribly so, if you're building the regex pattern
programmatically. That is, you can just line up the assertions one
after the other, like:

\s*
# no match on xmlns:h="http://www.mydomain.com" or xmlns:b="base"
(?!xmlns:h="http://www.mydomain.com")
(?!xmlns:b="base")
xmlns(:[^=]*)="[^=]*"

or

\s*
# no match on xmlns:h="http://www.mydomain.com" or xmlns:b="base"
(?!xmlns:h="http://www.mydomain.com" | xmlns:b="base")
xmlns(:[^=]*)="[^=]*"

But, the easiest way may be to use your original regex, with the
MatchEvaluator version of Regex.Replace. The MatchEvaluator delegate
would loom at it's argument, and return either "" or the match string
(no replacement,) depending on whether or not the match string is on
your 'preserve' list.
 
Perfect! Thanks again.

Jon Shemitz said:
jhgonzales said:
I cannot seem to formulate what seems should be an easy expression. I need to
find all occurances of a pattern except when it contains a specific string.
For example if the string is

Dim s As String = "xmlns:b=""base"" xmlns:h=""http://www.mydomain.com""
xmlns:t=""type"""

I need to find both xmlns:b="base" and xmlns:t="type" and replace them, but
not replace the xmlns:h="http://www.mydomain.com" occurance.

One approach is to use a "Zero-width negative lookahead assertion,"
like this:

\s* (?!xmlns:h="http://www.mydomain.com") xmlns(:[^=]*)="[^=]*"

RegexOptions.IgnorePatternWhitespace

This will get complicated, if you have a lot of patterns to ignore,
but not horribly so, if you're building the regex pattern
programmatically. That is, you can just line up the assertions one
after the other, like:

\s*
# no match on xmlns:h="http://www.mydomain.com" or xmlns:b="base"
(?!xmlns:h="http://www.mydomain.com")
(?!xmlns:b="base")
xmlns(:[^=]*)="[^=]*"

or

\s*
# no match on xmlns:h="http://www.mydomain.com" or xmlns:b="base"
(?!xmlns:h="http://www.mydomain.com" | xmlns:b="base")
xmlns(:[^=]*)="[^=]*"

But, the easiest way may be to use your original regex, with the
MatchEvaluator version of Regex.Replace. The MatchEvaluator delegate
would loom at it's argument, and return either "" or the match string
(no replacement,) depending on whether or not the match string is on
your 'preserve' list.
 
Back
Top