Reputation: 223
I have a set of strings coming in as:
I want to match it into 3 columns if Site*** presents at the start and if P*** presents in the end else match all of it
I am trying
^(.*)??(Site\S\s\d+\S\d+)?(.*)?\s(P.*?)$
But it is missing
How can I do it?
Regex: PCRE(PHP<7.3)
Upvotes: 0
Views: 51
Reputation: 163457
You can also use an optional non capture group to not capture the space and make the pattern a bit more specific:
^(Site:\h\d+\.\d+)?(.+?)(?:\h(P.*))?$
^
Start of string(Site:\h\d+\.\d+)?
Optional group 1, match Site:
a space and digits .
digits(.+?)
Capture group 2, match at last 1 or more times any char to not match an empty string(?:\h(P.*))?
Optional non capture group, match a space and capture P
and 0+ times any char in group 3$
End of stringUpvotes: 1
Reputation: 98
the last part, including \s
needs to be optional to match the string without it
try
^(Site\S\s\d+\S\d+)?(.*?)((\s)P.*)?$
https://regex101.com/r/SuvUDb/1
Upvotes: 2