Strikers
Strikers

Reputation: 4776

Reg Ex strictly match word start with a pattern

I'm trying to extract a text after a sequence. But I have multiple sequences. the regex should ideally match first occurrence of any of these sequences.

my sequences are

PIN, PIN :, PIN IN, PIN IN:, PIN OUT,PIN OUT :

So I came up with the below regex

(PIN)(\sOUT|\sIN)?\:?\s*

It is doing the job except that the regex is also matching strings like

quote lupin in, pippin etc.

My question is how can I strictly select the string that match the pattern being the whole word

note: I tried ^(PIN)(\sOUT|\sON)?\:?\s* but of no use. I'm new to java, any help is appreciated

Upvotes: 0

Views: 733

Answers (1)

Holger
Holger

Reputation: 298123

It’s always recommended to have the documentation at hand when using regular expressions.

There, under Boundary matchers we find:

\b          A word boundary

So you may use the pattern \bPIN(\sOUT|\sIN)?:?\s* to enforce that PIN matches at the beginning of a word only, i.e. stands at the beginning of a string/line or is preceded by non-word characters like space or punctuation. A boundary only matches a position, rather than characters, so if a preceding non-word character makes this a word boundary, the character still is not part of the match.

Note that the first (…) grouping was unnecessary for the literal match PIN, further the colon : has no special meaning and doesn’t need to be escaped.

Upvotes: 3

Related Questions