1

I want to find '106'. But it can't be in a longer number like 21067 nor can it be in a text string like y106M. But I also want to find it if it is at the beginning, or the end of line, or if it is the line.

So these lines are out:

106M
1106
106in a string is bad even if it starts the line
And it may finish with the target but not as the tail of a string106

but these are in:

106 something else
another 106
And of couse "106' will work as well
I just want 106 to show up but not embedded in other numbers or text
but it's OK is the end of the line is 106
106 may also start the line

I have tried the following search string and many variations of it:

(^|[^0-9A-Za-z])106($|[^0-9A-Za-z])

I'm new-ish to regex, and I likely don't have a handle on how the anchor characters work.

Display name
  • 1,228
  • 1
  • 18
  • 29

1 Answers1

2

You can just use \b word boundary assertion:

\b106\b

Demo

Or be more specific with lookarounds:

(?<=^|\W)106(?=$|\W)

Demo

or, as pointed out in comments, (?<!\w)106(?!\w) works too. Or (?<=[^a-zA-Z0-9])106(?=[^a-zA-Z0-9]) will work even better since _ is included in \w and \W set of characters.

dawg
  • 98,345
  • 23
  • 131
  • 206
  • Thank you. Do you also know a good resource for regex 'how-to' ? – Display name Apr 14 '17 at 16:35
  • I linked to one of them -- [rexegg.com](http://www.rexegg.com) [www.regular-expressions.info](http://www.regular-expressions.info/tutorial.html) is another. Best of luck! – dawg Apr 14 '17 at 16:43