1

I have some text that consists of space separated arbitrary-length codes, example:

AA *X IJ XYZ 7F *A OF *B 3C

As a first step, I want to extract all of the codes that are 2 characters where the first character is an asterisk, and the second character is one of 3 different alternatives (let's say they're A, B & C). So after this step, in this example I would have as my list of matches *A and *B.

I tried using the following regex

\b(\*[ABC])\b

...but of course, that doesn't work because the asterisk isn't considered part of a word. How can I do this in a readable, maintainable manner?

2
  • I'm glad you already have an answer but for my sake can you elaborate on the problem? Because I don't see the need for matching \b here when \s(\*[ABC])\s works just fine. Jan 31, 2013 at 4:00
  • @ByteBlast that wouldn't match at the start or end of the string....
    – Jon
    Jan 31, 2013 at 5:35

1 Answer 1

1

Use a positive lookbehind, and match either the begining of the string or a space character:

(<=^| )(\*[ABC])\b

See it here in action: http://regex101.com/r/wS5qS2


If all you care about is the capture group, you don't need the lookbehind. Just keep it out of your capture group:

(?:^| )(\*[ABC])\b
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.