-2

Not too familiar with regex so could use some pointers in the right direction if possible.

I have some possible String values which can be something like the following:

"88976756 ABC 33ddf33a24"

"89999ABC 3hhhj33"

"7ffhh7AB C78788sd"

What I need is to find if a value ABC exists in those Strings but is not preceded or followed by an alphanumeric character.

In the above examples, only the first should return ABC. The second example is preceded by a digit and the third has a space in the middle.

If anybody knows of a way to do this or has some documentation around the best way to do it I'd appreciate it.

Edit: The Strings above were probably a bit simplistic. Some further examples below

"67676/'ABC'7866cc"

ABC should be found as there is no alphanumeric character before or after it

"88xx#'\A2C"

A2C should be found as there is no alphanumeric char before or after

"88xx# A2C&&&88"

A2C should be found as there is no alphanumeric char before or after

"88xxA2C&&&88"

A2C should not be found as there is an alphanumeric char before it

Thanks

Predz
  • 201
  • 1
  • 7
  • 17
  • Not really, updated possible example above to clarify – Predz Mar 13 '17 at 10:26
  • 1
    All your cases shown are covered by word boundaries. Only issue might be `_ABC_`. If This shall not be matched, you'd need to use some custom boundaries, e.g. `(?<!\w)ABC(?!\w)`. Oh and please format your code as code. – Sebastian Proske Mar 13 '17 at 10:30

1 Answers1

1

Use the word boundary matcher \b

Your regex could be as simple as

\bABC\b
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • suppose instead of ABC I had A23. Can word boundries match on alphanumeric strings? Also, the full String to run the regex on can have any compination of characters, the ones posted above are probably a bit over simplified. I'll update to include more examples – Predz Mar 13 '17 at 10:19