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.
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:
Regular expression to match a line that doesn't contain a word
(34 answers)
How to negate specific word in regex? [duplicate]
(12 answers)
Closed 3 years ago.
I want to validate a string with alpha numeric values, but if the string contains -- (double dash) anywhere in the string, it should be invalid.
valid:
apple123
-apple-123
app-le123
a-p-p-l-e-1-2-3
invalid:
--apple123
app--le123
https://stackoverflow.com/a/1240365/1920590
The above old post have the answer ^(?!.*bar).*$ which does the negation, but it does not work for same character repetition like --.
Can anyone help me to figure out to modify the ^(?!.*bar).*$ to identify -- as a string.
You may use a negative lookahead:
^(?!.*--)[\w-]+$
(?!.*--) is a negative lookahead assertion that will fail the match if -- appears anywhere in input.
[\w-] matches a word character [a-zA-Z0-9_] or a hyphen
RegEx Demo
This question already has answers here:
regex: required character in brackets
(3 answers)
Closed 4 years ago.
I am working for something and writing a regular expression to capture a string which is either (numbers and letters) or only numbers.
I know a regex for only number is [0-9] and alphanumeric is [A-Za-z0-9] . But this would capture even the strings which are only letters. How do i force it to not have only letters? Is there a way to do it in a single regex?
([0-9]*[a-zA-Z]*[0-9])+([a-zA-Z]*)
This should solve your problem.
You can test it here
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)/
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 6 years ago.
I have tried to match <b> using regular expressions.
the pattern /<.>/ matches <b> but the pattern /<[.]>/ does not match.
What is the difference between /<.>/ and /<[.]>/ in regular expressions
When you put dot inside [] it is a literal dot, not a any character.
So the /<[.]>/ matches only <.>. It is the same as escaping a dot: \. in regexp.