This question already has answers here:
Regex - Does not contain certain Characters
(2 answers)
Regex: match everything but a specific pattern
(6 answers)
Replace chars if not match
(2 answers)
Closed 4 years ago.
How to do we compose a regex that says "the matches fails if there's a dash somewhere in the middle"
I have several lines that are composed as 3958.3r - 5v and some are without the dash for example: 3958.3v4r. I am able to get the ones with the dash, but not only the ones without the dash
This can be accomplished with a character class negation. ^ at the beginning of a character class simply negates the character class. If you then only have the character -, then you create a character class that matches anything but -.
^[^\-]+$
According to what you have said, you need to put the ^ (start of string) at the front of your pattern, and escape the . or else you will match not only the . after 395[0-9], but any character at all like 3950z.
^395[0-9]\.[^-]+$
Related
This question already has answers here:
typescript Yup validation or condition with regex using matches problem
(2 answers)
Closed 11 months ago.
I'm trying to make sure only one type of special character (semi-colon, comma, or space) is used in a string.
e.g this should match as it only uses one type of special character (semi-colon): https://hello.com/example1;https://hello.com.com/example2;https://hello.com.com/example3
This should fail as it mixes two types of special characters (space and semi-colon)
https://hello.com/example1; https://hello.com.com/example2 ;https://hello.com.com/example3
This is my code - im using the yup schema builder for validation:
const myValidation = yup
.string()
.matches(/^([A-Za-z0-9://,\\.]|[A-Za-z0-9:// \\.]|[A-Za-z0-9://;\\.])+$/, 'Please separate each with a comma, space or semicolon')
.required();
When i only have /^([A-Za-z0-9://,\\.]+$/ it works correctly to only match the string if it has a only a comma as special character: https://hello.com/example1,https://hello.com.com/example2,https://hello.com.com/example3
but as soon as i add the other or conditions /^([A-Za-z0-9://,\\.]|[A-Za-z0-9:// \\.]|[A-Za-z0-9://;\\.])+$/ it starts allowing for semi-colon and space and comma special characters in the string at the same time (the invalid case)
You can use
/^[A-Za-z0-9:\/\\.]+(?=([,;\s])|$)(?:\1[A-Za-z0-9:\/\\.]+)*$/
See the regex demo. Details:
^ - start of string
[A-Za-z0-9:\/\\.]+ - one or more ASCII letters, digits, :, /, \ or . chars
(?=([,;\s])|$) - a positive lookahead capturing the subsequent ,, ; or whitespace into Group 1, or matching end of string
(?:\1[A-Za-z0-9:\/\\.]+)* - zero or more sequences of the Group 1 value and then one or more ASCII letters, digits, :, /, \ or . chars
$ - end of string.
This question already has answers here:
How to use double brackets in a regular expression?
(2 answers)
Difference between \w and \b regular expression meta characters
(5 answers)
Difference between * and + regex
(7 answers)
Closed 4 years ago.
How to capture any alphabet and any numeric into one group using regex.
I use basic regex to capture it, but every single character become a new match.
According to image below, I want the result like this:
match 1 = Q
match 2 = m
match 3 = C
match 4 = t2
match 5 = result (this word 'result' just an example, it can be replaced by any word)
Use a quantifier + for one or more
/[[:alnum:]]+/
See demo at regex101
Be aware that \n matches a newline and not a number. \d would be the short for digit.\w (word character) matches [A-Za-z0-9_]. It includes \d already .
This question already has answers here:
Regex how to match an optional character
(5 answers)
Closed 6 years ago.
I just want to write a regular expression 4 digits and '.' and 5 digits and optional 'A'
Ex: 1111.2345A where A is optional.
^[0-9]{4}[\.][0-9]{4}$
This reg ex will give 1111.2345, but how to add Optional 'N' at last.
Use ? at the end for characters:
[A-Za-z]?
This will match at most 1 presence of a character (lower or upper case).
You can check for a character zero or one times with this:
'[A]{0,1}'
Put that at the end of your string and it will try and match the character 'A' zero or one times. You may also use the symbol ? to match zero or one times. All about preference.
To get a single, optional A at the end, append A? to your regular expression:
^[0-9]{4}[\.][0-9]{4}A?$
Btw. instead of [0-9] you could use \d which stands for 'digit':
^\d{4}\.\d{4}A?$
This question already has answers here:
Reference - What does this regex mean?
(1 answer)
Using explicitly numbered repetition instead of question mark, star and plus
(4 answers)
Closed 3 years ago.
I've been searching for a long time but didn't find an answer for my question, can tell me what the meaning of
(?:[-\w\d{1-3}]+\.)+
and not
(?:[-\w\d{1,3}]+\.)+
I don't understand the {1-3} part and can't find anywhere what it's mean.
Thank you
Everything between [] are characters to be matched. So it matches each of those characters:
- the literal character -
\w match any word character [a-zA-Z0-9_]
\d match a digit [0-9]
{ the literal character {
1-3 a single character in the range between 1 and 3
} the literal character }
the 1-3 makes no sense there, as well as the \d. Both are included in \w
Even what you would say that is correct {1,3} inside the [] makes no sense.
This question already has answers here:
Regex for validating a string with pre-specified special characters
(3 answers)
Closed 8 years ago.
I have unsuccessfully be looking around the web for such a simple regex and can't seem to put it together.
I need a regex which allows any letter, numbers, whitespace and particular special characters only (# # $ & ( ) - _ /)
"Test #123 #Sample/test" is valid
"Test ^ £300" is not valid
Simply you could try the below regex,
^[\w\s##$&()\/-]+$
DEMO
^ Asserts that we are at the start.
[\w\s##$&()\/-]+ Matches one or more characters from the list.
$ Asserts that we are at the end.