0

I am needing to find whether the words root, name, or server are in a line with Regex so I can ignore them, but I have not found a good solution to identify them yet.

The format of what it would scan through is like this:

<Server name="JZL902757">
   <Snapshots timestamp="02/10/2022 12:16:31">
      <Snapshot root="D:\somefolder\anotherfolder">

All three words will always have a space preceding them and a "=" coming after.

I found this Regex to match string containing two names in any order in trying to find a solution but it did not yield any matches when testing it.

Any help or suggestions are appreciated.

jakebake
  • 91
  • 7

1 Answers1

0

I'm not sure to have understood what you want so I give you all the options.

This string will match any line containing one of the three words: server, string or name so it uses | for OR

^.*([Nn]ame|[Ss]erver|[Rr]oot).*$

This string will not match any line containing none of: server, string or name:
we have a negative look ahead for each one so it is NOR

^((?![Rr]oot)(?![Ss]erver)(?![Nn]ame).)*$

And this string will only match lines containing all three words, in any order, so it is AND using three positive look-aheads.

^(?=.*[Ss]erver)(?=.*[Rr]oot)(?=.* [Nn]ame).*$

and lastly this string will only match lines containing all three words, with a space before and an equals sign after each one

^(?=.* [Ss]erver=)(?=.* [Rr]oot=)(?=.* [Nn]ame=).*$