Regex match exact words
Solution 1
Your pattern tries to match a ?
that is preceded with a word char, and since there is none, you do not have a match.
Use the following pattern:
'/\?v(?:er)?\b/'
See the regex demo
Pattern details:
-
\?
- a literal?
char -
v(?:er)?
-v
orver
-
\b
- a word boundary (i.e. there must be a non-word char (not a digit, letter or_
) or end of string afterv
orver
).
Note you do not need the first (initial) word boundary as it is already there, between a ?
(a non-word char) and v
(a word char). You would need a word boundary there if the ?
were optional.
Solution 2
Try the following regex pattern;
(\?v(?:\b|(?:er(?!sion))))
This will allow ?ver
and ?v
, but will use a negative look-ahead to prevent matching if ?ver
is followed by sion
, as in your case ?version
.
Solution 3
Building upon above answers, to match a word without being a part of another you can try
\b(WORD_HERE)\b
which in your case is \b(\?ver)\b
this will allow ver
and prevent version
average
Fjott
Updated on June 14, 2022Comments
-
Fjott almost 2 years
I want my regex to match
?ver
and?v
, but not?version
This is what I have so far:
$parts = preg_split( "(\b\?ver\b|\b\?v\b)", $src );
I think the trouble might be how I escape the
?
.-
mickmackusa almost 7 yearsPlease add two or three sample inputs so that future SO readers can see what you are asking in context.
-
Casimir et Hippolyte almost 7 yearsWhat you call "exact words" isn't very clear, in particular since
?
isn't a word character. You should explain what exactly delimits a word. -
Casimir et Hippolyte almost 7 years@mickmackusa: It doesn't matter, It's always unclear (fortunately!).
-
mickmackusa almost 7 years@Fjott Please show some sample data, because this could determine whether a pattern is correct or not.
-
-
mickmackusa almost 7 yearsHow do you know the targeted substring terminates at the end of line? Your pattern could actually be incorrect, but we will not know until the OP provides some sample strings. This is not a concern for Wiktor's correct pattern.
-
Tom Wyllie almost 7 yearsOP did provide sample strings - he said "I want my regex to match
?ver
and?v
, but not?version
". -
Tom Wyllie almost 7 yearsHowever, on the off-chance that that is what OP meant and they didn't articulate it very well, I've edited my answer now as it comes at no cost to the original solution. Thanks for pointing that out! :)
-
mickmackusa almost 7 yearsNow that you understand the requirement for a word boundary, your answer is now a less efficient version of Wiktor's answer and is not useful. You did not attempt to optimize your pattern and so you are not showing users a refined pattern.
\?v(?:\b|(?:er(?!sion)))
and\?v(?:\b|er(?!sion))
are more efficient versions of your posted pattern. -
Toto over 4 yearsWhy using a capture group? The word boundary before
\?
is useless and can cause wrong match if it is positionned at the beginning of the string or if there are spaces before (i.e. it will no match the string?ver
Demo