This question already has answers here:
What does ?! mean?
(3 answers)
Closed 6 years ago.
$str = "doctor_who:eeh1234LMNOP51234.123";
I want to match doctor_who:ee not doctor_who:eeh;
$str =~ m/doctor_who:e(?!eh)[epx];
I want to know the role of regular expression within Parentheses;
A component like (?! ... ) will fail to match if the following characters in the target string match the enclosed regex pattern. It's called a negative look-ahead
It's unclear whether you need help to form a pattern to your requirements, or if you've come across a pattern that you don't understand
If I was writing it, I'd look at your specification
I want to match 'doctor_who:ee' not 'doctor_who:eeh'
You want to match doctor_who:ee that isn't followed by h, which is
/doctor_who:ee(?!h)/
Related
This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 2 years ago.
I am currently using the following character class:
[^\)\(] in my regex
I want to add the word 'hello' to this class so it is also not matched in my string.
I have tried
[^\)\((hello)]
but it does not work.
What can I do?
One typical way you would enforce that hello does not appear would be to use a negative lookahead, e.g.
^(?!.*hello)[^t()]+$
If you only wanted to exclude hello when it appears as a bona fide word, then surround it with word boundaries in the lookahead:
^(?!.*\bhello\b)[^t()]+$
This question already has answers here:
Regex plus vs star difference? [duplicate]
(9 answers)
RegEx match open tags except XHTML self-contained tags
(35 answers)
Closed 4 years ago.
I am trying to match a string "menu-item" but has a digit after it.
<li id="menu-item-578" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-578">
i can use this regex
menu-item-[0-9]*
however it matches all the menu-item string, i want to only match the "menu-item-578" but not id="menu-item-578"
how can i do it?
thank you
You should avoid using menu-item-[0-9]* not because it matches the same expected substring superfluously but for the reason that it goes beyond that too like matching menu-item- in menu-item-one.
Besides replacing quantifier with +, you have to look if preceding character is not a non-whitespace character:
(?<!\S)menu-item-[0-9]+(?=["' ])
or if your regex flavor doesn't support lookarounds you may want to do this which may not be precise either:
[ ]menu-item-[0-9]+
You may also consider following characters using a more strict pattern:
[ ]menu-item-[0-9]+["' ]
Try it works too:
(\s)(menu-item-)\d+
https://regex101.com/
\s Any whitespace character
Use a space before, like this:
\ menu-item-[0-9]*
The first ocurrence has an " right before, while the second one has a space.
EDIT: use an online regex editor (like Regex tester to try this things.
This question already has answers here:
Regex for matching something if it is not preceded by something else
(3 answers)
Closed 3 years ago.
I want to match strings containing MYSTUFF in all cases, except when MYSTUFF is preceded by ENABLED_. So that the following would match:
MYSTUFF
m_MYSTUFF
MYSTUFFIsGreat
allOf_MYSTUFF
but the following wouldn't:
ENABLED_MYSTUFF
m_ENABLED_MYSTUFF
ENABLED_MYSTUFFIsGreat
allOf_ENABLED_MYSTUFF
I tried several variations using negative lookahead (variations of \w*(?!.*ENABLED_)MYSTUFF\w*), and conditional (variations of (?(?!=ENABLED_)(MYSTUFF))), but I did not manage to get the results I'm after.
Is what I want even doable with regexes?
You could accomplish this by using a negative look-behind assertion ...
\w*(?<!ENABLED_)MYSTUFF\w*
see regex demo
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 8 years ago.
What is the use of
?=
in perl regex
please tell the exact meaning and give some regex example.
(?=...)
is a positive lookahead, a type of zero-width assertion. What it's saying is that the match must be followed by whatever is within the parentheses but that part isn't captured.
Example:
.*(?=bar)
This pattern matches all the characters upto the string bar. When bar is detected then it stops matching. If a line contains more than one bar means it matches upto the last bar because .* does a greedy match.
DEMO
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Regular expression to match string not containing a word?
How can I invert a regular expression in JavaScript?
Say I have the regex foo123. How do I match everything that is not foo123?
Use negative lookahead for this.
(?!foo123).+
matches any string except foo123
If you want to match empty string also, use (?!foo123).*
In your case (according to the comment) the required regex is (?!P[0-9]{1,}).+.
It matches P and 123, but not P123.