This question already has answers here:
How to negate specific word in regex? [duplicate]
(12 answers)
Closed 7 years ago.
I am trying to learn RegEx and build a regular expression that would look whether specified word is NOT in the provided string. So far I did try Regular Expression Info and RexxEgg all this tested on Regular Expression Online but I did not find the answer to my question.
I have tried conditionals and lookarounds. Let's say I want to build an expression to test against not existing word myword and pass expression when the word is NOT in the string. I used expression
(?(?!myword).*)
but RegEx passes regardless the word myword meaning both strings This is the text and This is myword the text pass the test.
Using negative lookahead and conditions is used to test that condition is true when myword does not exist. Lookahead is also zero length and therefore .* would return the whole string.
Hope someone can help :)
^(?(?!\bmyword\b).)*$
You can try this.See demo.Also use \b for matching exactly myword and not mywords
https://regex101.com/r/hI0qP0/7
You should use anchors and negative lookahead:
^(?!.*?myword).*$
(?!.*?myword) is a negative lookahead that will fail the match if myword is found anywhere in the input string.
Related
This question already has answers here:
RegEx for allowing alphanumeric at the starting and hyphen thereafter
(4 answers)
Closed 5 years ago.
I want to build a regular expression which only matches [A-Za-z0-9\-] with an additional rule that hyphens (-) are not allowed to appear at the start and at the end.
For example:
my-site is matched.
m is matched.
mysite- is not matched.
-mysite is not matched.
Currently, I've come up with ^[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]+$.
But this doesn't match m.
How can I change my regular expression so that it fits my needs?
Use look arounds:
^(?!-)[A-Za-z0-9-]*(?<!-)$
The reason this works is that look arounds don't consume input, so the look ahead and the look behind can both assert on the same character.
Note that you don't need to escape the dash within the character class if it's the first or last character.
This question already has answers here:
A Regex that will never be matched by anything
(30 answers)
Closed 5 years ago.
I am studying regex lookaround from this tutorial.
It has an example explaining how lookaround are used to check the existence (or non-existence), but the regex inside lookaround parentheses is not used for actual match.
In example, the pattern q(?=u)i is checked against String quit. And it doesn't return a match.
I understood the example.
But I can't think of any string that matches this regex pattern. If my understanding of lookaround is correct I think there isn't any String that matches with this regex.
Am I correct? If not, which String matches this regex?
I'm not a big fan of this tutorial site, but if you read closely what it actually says, it never claims that the regex q(?=u)i matches the string quit:
Let's take one more look inside, to make sure you understand the implications of the lookahead. Let's apply q(?=u)i to quit. The lookahead is now positive and is followed by another token. Again, q matches q and u matches u. Again, the match from the lookahead must be discarded, so the engine steps back from i in the string to u. The lookahead was successful, so the engine continues with i. But i cannot match u. So this match attempt fails. All remaining attempts fail as well, because there are no more q's in the string.
I think you might still be confused about how lookaheads work. Either that, or you misread the tutorial site. If the former, then lookaheads work by asserting a match, without actually consuming anything in the string. So the regex q(?=u)i says to:
match the letter 'q'
lookahead to the next character after 'q' and assert that it is 'u'
then match an 'i' immediately after the 'q'
Of course, the string 'quit' fails, and in fact all strings would fail. The lookahead says to verify that q is followed by u, but the following pattern contradicts this by insisting that i is what follows.
This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 6 years ago.
I would like to use (pure) regex to match strings that do not contain the word word.
However, I would not like to use lookaround, balancing groups, or that kind of stuff.
If it is impossible, then can we match strings that do not start with word instead?
Examples
word should not match.
wor should match.
wore should match.
use this pattern
^.*\bword\b.*$|(.+)
match strings that has word first, then match and capture strings that don't
Demo
Depending on your engine, you could use this pattern
^.*\bword\b.*$(*SKIP)(*F)|(.+)
Demo
This question already has an answer here:
How to implement regular expression NFA with character ranges?
(1 answer)
Closed 7 years ago.
I need to implement positive and negative ranges in my regex matcher.
It looks not difficult for positive range:
[1-3] == (1|2|3)
But I do not understand how to convert negative range [^1-3] to simple regex string.
Is it possible?
Thanks!
Update
Not. Seems it is impossible.
Ok, how regex libraries process negative ranges in this case?
If the regex engine you're using supports negative lookahead, you can do it like this:
(?!1|2|3).
?! is the negative lookahead operator. It says "the characters that follow this expression must not match this expression." It makes a negative match without advancing the cursor. Here it's followed by a . to indicate any character.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
A Regex that will never be matched by anything
I have a script that takes a regex as a parameter. By default I want to set the regex to something that will never match any string, so I can simply say
if ($str =~ $regex)
without e.g. having to check defined($regex) first.
I came up with
qr/[^\s\S]/
but don't know if this will match some utf8 character that is neither a space nor a non-space.
/(?!)/
http://perl.plover.com/yak/regex/samples/slide049.html
Combine a negative lookahead for an arbitrary character followed by a match for that character, e.g.
/(?!x)x/
Works on all the test cases I threw at it. Here are some tests on rubular.
/ ^/ seems to do, and is short(est).