Reputation: 3
I am using a php regular expression to try and solve an advanced text search query in mysql. The query needs to group smaller things to make them work properly. Currently I can handle things like "foo 7S" and group them together if I see the word "foo".
Currently, my regex looks as such
/(foo|bar|baz faz)\s+([a-zA-Z0-9]+)\b/i
However, if I am trying to search for something specific like "foo 7.10", then the ".10" is split out instead of being combined. My current regular expression is
/(foo|bar|baz faz)\s+([a-zA-Z0-9]+)(\.[a-zA-Z0-9]+)\b/i
This works for "foo 7.10" but doesn't match anything for "foo 7" or "foo 7S". Any help would be appreciated.
Upvotes: 0
Views: 89
Reputation: 370729
You just need to make the group that captures the decimal part optional:
(foo|bar|baz faz)\s+([a-zA-Z0-9]+)(\.[a-zA-Z0-9]+)?\b
^
https://regex101.com/r/QBFeuP/1
Note that since you're using the i
flag, the regex is case-insensitive, so there's no need to use a-zA-z
- those can be reduced to just a-z
:
(foo|bar|baz faz)\s+([a-z0-9]+)(\.[a-z0-9]+)?\b
and if there is no chance of underscores being there, you can use \w
instead:
(foo|bar|baz faz)\s+(\w+)(\.\w+)?\b
in which case the final \b
is unnecessary, because both of the \w+
s are greedy and will match as many characters as they can, which implies that the following position will necessarily be a word boundary.
You also might consider putting a word boundary at the very beginning, if it would be helpful:
\b(foo|bar|baz faz)\s+(\w+)(\.\w+)?
Upvotes: 2