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.
Related
This question already has answers here:
Regular expression for a string containing one word but not another
(5 answers)
Closed 2 years ago.
I lack experience with regex and need help with this problem:
Objective: Create a regex pattern that matches "renal" or "kidney" in an arbitrary string only if it does not contain "carcinoma".
Example strings:
"Renal cell carcinoma"
"Clear cell carcinoma of kidney"
"Chronic renal impairment"
Expected output: The regex pattern does not match "renal" and "kidney" in the first two strings; it does match "renal" in the third string (since there is no "carcinoma").
What I've tried: (?<!carcinoma).*(kidney|renal). I stopped here because it didn't work — because, as I've learned here and here, lookbehinds are limited to basically non-zero length; regular expressions cannot be applied backwards an arbitrary length.
So what regex pattern will do the trick? I want a pattern that maintains focus on (or is "anchored" on) "renal" and "kidney" and not "carcinoma".
The pattern that you tried (?<!carcinoma).*(kidney|renal) asserts what is directly to the left is not carcinoma which is true from the start of the string.
Then it will match any char 0+ times until the end of the string and tries to backtrack to fit in either kidney or renal.
Instead of using (?<!carcinoma), use ^(?!.*\bcarcinoma\b) to assert from the start of the string that bcarcinoma is not present at the right.
Then match either the word renal or kidney in the string.
^(?!.*\bcarcinoma\b).*\b(renal|kidney)\b.*
Regex demo
This question already has answers here:
Regex for matching something if it is not preceded by something else
(3 answers)
Closed 2 years ago.
I am trying to match ) if there is a ( and two numbers to the left of it.
Example "(55)"
I want to match ) in "(55)"
I do not want to match "(hello world 55)"
I currently have the following as my regex:
\(\d+\)
It matches "(55)" but I just want the ) in it. Is there a way to get a certain character by placement in a regex? Or do you have a better solution?
There are multiple ways to match what you want.
For instance, you can use \K to rest the previous match:
\(\d+\K\)
Using positive lookbehind
(?<=\(\d+)\)
Also capturing the content for the match
\(\d+(\))
Flutter and all Languages supported
Insert all Special Character, Number, Lowercase & Uppercase
condition 1: Must Length of word 8
condition 2: Insert any of above given list (Not Must in all type)
r'^([a-zA-Z].)*.{8,}$' ,
Must Insert Atleast One in Above given list,
r'^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]+!##\$&*~{8,}$',
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.