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
Related
This question already has answers here:
Regex not to allow double underscores
(3 answers)
Closed 3 years ago.
I have tried different regular expressions already but I am not sure how to have it catch one or more underscore. If are two together, must be invalid.
First word must be capital letter, then any character, the problem is underscore
I have this: (^[A-Z])(\w{6,30} ?=*(_))
This regex may work for you with a negative lookahead condition:
^[A-Z](?![^_]*__)\w{6,30}$
(?![^_]*__) is a negative lookahead condition that fails the match if __ appear anywhere after first capital letter.
RegEx Demo
If you mean a pattern which is a word starting with a capital letter followed by some groups consisting of a single underscore and a word:
^[A-Z]\w{6,30}(_\w{6,30})*$
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:
regular expression for anything but an empty string
(9 answers)
Closed 4 years ago.
I have the following code
https://stackblitz.com/edit/angular-uvxifq-qrjtpg
pattern="^\s+$"
I need to negate this, because as of now I got error when I put a letter, but it should only show error IF there is only white space.
I tried using ?! but it shows an error
You can use the following regex:
^(?: *[^\s] *)+$
demo: https://regex101.com/r/th0A3A/3/
If you want to use a negative lookahead (?!, you could check if from the start till the end of the string there are no whitespace characters:
pattern="^(?!\s*$)[\s\S]+$"
Regex demo
That will match
^ Start of string
(?!\s*$) Negative lookahead, assert that what follows is not 0+ whitespace characters and end of string
[\s\S]+ Match 1+ times any character including new lines
$ End of string
This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 8 years ago.
I need a regexp that will match everything except a single constant (case ignored)
Example for constant ALL, should match words like: dog, MOUSE, mall, alligator. But it shouldn't match: all, ALL, alL.
(?si)^(?!all$).*
will match any string except all (case-insensitively).
(?i) makes the regex case-insensitive, (?s) allows the dot to match any character, including newlines. If you don't expect newlines in your input, you can remove the s.
See it live on regex101.com.
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.