-1

I'm trying to do a regular expression matching with REGEXP_LIKE and I'm looking for a regexp to find if the value of a specific tag is not a specific string. For example:

<person>
   <name>John</name>
   <age>40</age>
</person>

My goal is to validate that the name tag's value is not John, so the REGEXP_LIKE would return true for input xmls where name is not John. Thank you in advance for the help!

Fragosus
  • 5
  • 2

1 Answers1

0

A quick and easy way to do this is to simply negate the regex search:

... WHERE NOT REGEXP_LIKE('column_name', '<name>John</name>')

However, as should be mentioned every time a question like this is posted, it's generally a bad idea to parse XML with regex. If you find yourself constructing more complex regex patterns to search this XML data, then you should:

  • Use an XML parser instead of regular expressions, or
  • Change how you are storing the data! Make person.age a separate table column; don't bung the entire XML structure into a single place.
Tom Lord
  • 27,404
  • 4
  • 50
  • 77
  • I used the same solution you suggested, however I was curious in finding a way to see if negate can be done inside the regex. – Fragosus Jun 14 '17 at 10:03
  • This is easily done with look-aheads, but I don't know if `PL/SQL` regular expressions support this. Something like: `(?!John)[^<]*` ... But as I say, using regular expressions to parse XML is asking for trouble. – Tom Lord Jun 14 '17 at 10:21
  • If the language does not support this regex feature, then you could also achieve this with a much more verbose pattern: `([^J]|J[^o]|Jo[^h]|Joh[^n]|John[^<])[^<]*` – Tom Lord Jun 14 '17 at 10:24
  • Thank you very much! The first solution (?!John)[^<]* works as a charm. – Fragosus Jun 14 '17 at 10:43