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 .
Related
This question already has answers here:
Negating a backreference in Regular Expressions
(6 answers)
Closed 2 years ago.
I'm trying to exclude strings where the first char is equal to the third char:
passing strings:
X9K3
V3Z5
not passing strings:
A4A9
R5R1
I tried ^(.).[^\1].$
but I'm too new to regex to understand why it failed.
You may use this regex:
^(.).(?!\1).+$
RegEx Demo
[^\1] does not do what it intends to because inside [...] every character becomes literal after first ^ so it just matches everything except \ and 1.
(?!\1) on the other hand is a negative lookahead which fails the match if character on next position is not same as what we captured in group #1.
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]\.[^-]+$
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:
How to validate phone numbers using regex
(43 answers)
Closed 7 years ago.
Regex:
\b(\(\d{3}\)|\d{3})?[-.]?\d{3}[-]?\d{4}\b
My input file has two types of phone numbers. One, whose first 3 digits are enclosed in parenthesis and the other with no parenthesis.
Eg:
"(201)-450-4479" ,"234-345-3456"
I want to match both type of phone numbers using alternate operator.
Please suggest me. What modification is required for above mentioned expression to get the intended result?
\b matches at a word-nonword boundary. If such a boundary should appear before (, it must be preceded by a word character, not whitespace or nothing.
Cf.
print /\b\(/ ? 1 : 0 for '(', ' (', 'a(';
Remove the starting \b from the regex, or replace it with
(?x: \b | \s | ^ )
I'd use this:
(\(?\d+\)?\-\d+\-\d+)
or using the alternate operator:
(\d+\-\d+\-\d+|\(\d+\)\-\d+\-\d+)
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.