0

I would appreciate help with one regex.

I have a string of ids example "123,55,68,890,456,333,168"

How should the regex look like to find a specific id -- example 68? Bear in mind that 168 shouldn't be returned. There are four cases where the id could be positioned:

  1. x (only one id in the list/string)
  2. ,x, (somewhere in the middle of the string)
  3. x, (at the beginning of the string)
  4. ,x (at the end of the string)

I would use this regex as part of the SQL query.

Thanks in advance

1 Answers1

-1

/(?:^|,)number(?:$|,)/ should do the trick.

(?:) is a non-capturing group. It looks for the value in the parentheses after the :, but doesn't include it in your result.

So this regular expressions says "Find number that is at the beginning or preceded by a comma, and at the end or followed by a comma."

Michael Landis
  • 1,074
  • 7
  • 12