Home > OS >  Return unique results (i.e. no duplicates) from a regex query
Return unique results (i.e. no duplicates) from a regex query

Time:11-19

How can I prevent this regex query (?<=NASDAQ:).* from showing duplicate results?

If it matches a result, for example "NEGG", I would like to highlight it just the one time.

I've set up an example here at regex101.com Example of regex

Would appreciate if anyone could help me with this.

Thanks.

CodePudding user response:

Assuming the regex engine used is either JS (new one ) or C#.
Both of which would allow you to use a well placed, variable length look behind.
This will allow to highlight only the first unique NASDAQ codes.

(?<=NASDAQ:)(?=(\w ))(?<!^[\s\S]*NASDAQ:\1[\s\S]*)\1

https://regex101.com/r/EeGy3E/1

 (?<= NASDAQ: )
 (?=
    ( \w  )                       # (1)
 )
 (?<! ^ [\s\S]* NASDAQ: \1 [\s\S]* )
 \1
  • Related