0

This is my reg expression that find it

(<instance_material symbol="material_)([0-9]+)(part)(.*?)(")(/)(>)

I need to find a string that does not contain the word "part" and the xml lines are

<instance_material symbol="material_677part01_h502_w5" target="#material_677part01_h502_w5"/>
     <instance_material symbol="material_677" target="#material_677"/>  
  • 1
    Possible duplicate of [Regex: match everything but specific pattern](https://stackoverflow.com/questions/1687620/regex-match-everything-but-specific-pattern) – 41686d6564 Feb 27 '19 at 19:06

3 Answers3

1

You can use negative lookahead

^(?!.*part).*?$
  • ^ - start of string.
  • (?!.*part) - condition to avoid part.
  • .*? - Match anything except new line.
  • $ - End of string

Demo

zx485
  • 24,099
  • 26
  • 45
  • 52
Code Maniac
  • 33,907
  • 4
  • 28
  • 50
0

Many regex starters will encounter the problem finding a string not containing certain words. You could find more useful tips on Regular-Expression.info.

^((?!part).)*$
Joseph
  • 70
  • 14
0

You need to be aware that all attempts to process XML using regular expressions are wrong, in the sense that (a) there will be some legitimate ways of writing the XML document that the regex doesn't match, and (b) there will be some ways of getting false matches, e.g. by putting nasty stuff in XML comments. Sometimes being right 99% of the time is OK of course, but don't do this in production because soon we'll have people writing on SO "I need to generate XML with the attributes in a particular order because that's what the receiving application requires."

Your regex, for example, requires the attribute to be in double rather than single quotes, and it doesn't allow whitespace around the "=" sign, or in several other places where XML allows whitespace. If there's any risk of people deliberately trying to defeat your regex, you need to consider tricks like people writing &#112; in place of p.

Even if this is a one-off with no risk of malicious subversion, you're much better off doing this with XPath. It then becomes a simple query like //instance_materal[@symbol[not(contains(., 'part'))]]

Michael Kay
  • 138,236
  • 10
  • 76
  • 143