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?
Use a positive lookbehind, and match either the begining of the string or a space character:
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: