suj suj - 4 months ago 15
Bash Question

RegEx matching multiple exact strings

I have a string:

MPCP-1234 this is test SAF-1234 this is testing


I need to match
MPCP-1234
and
SAF-1234
.

My regEx is

SAF|MPCP-[0-9][0-9][0-9][0-9]


But it is only finding
MPCP-1234
and
SAF
(not
SAF-1234
). I need to match both.

Regex101 link

Answer

The problem you're having is the alternative "operator" (|). The regex you've posted corresponds to "SAF" OR "MPCP-<4-digit number>".

What you want is (?:SAF|MPCP)-\d{4}. The \d matches any digit, and {4} means "exactly 4 of the last token (in this case, \d)". The (?:) means "non-capturing group".

To explain your problem a little better, the alternation character is limited to the current group - if there is no explicit parenthesized group, the entire pattern is used as an implicit group.