Regular expression to match ) under certain conditions [duplicate] - regex

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,}$',

Related

Regex first char is different than third char [duplicate]

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.

Regex pattern alternative for (negative) lookbehinds of arbitrary, non-zero length [duplicate]

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

If Else regex matching [duplicate]

This question already has answers here:
regular expressions: match x times OR y times
(4 answers)
Closed 3 years ago.
I want to build a regex where it searches for a string containing 12 digits in a row. If there's no match, look for a string with only 10 digits in a row.
For example:
a123456789012a
a1234567890a
Would return:
123456789012
And if the input is:
a1234a
a1234567890a
It would return:
1234567890
I managed to create the regex for the individual operations, beeing (?<!\d)\d{10}(?!\d) for 10 digits and (?<!\d)\d{12}(?!\d) for 12 digits, but I can't group them up in a if-else style.
I tried the following:
(?(?<!\d)\d{12}(?!\d)|((?<!\d)\d{10}(?!\d)))
but if the first pattern don't match, the regex don't try to match the second, returning nothing
You can use a simple regex like this:
\d{12}|\d{10}
working demo
Look that I have not used multiline nor global flags. This way the pattern is going to find the first match you want.
Case 1:
Case 2:
BTW, use capturing groups if you want to capture the content:
(\d{12}|\d{10})

Regex: How to remove FQN [duplicate]

This question already has answers here:
Regex match everything up to first period
(3 answers)
Closed 4 years ago.
I want to remove the domain information from hostnames.
E.g. I want "server1.mydomain.com" to be just "server1".
I thought I had it with:
^(\w*)
But then I realized I also have hostnames like "desktop-1.mydomain.com", and they all got changed to "desktop" and not "desktop-1" etc.
Any suggestions how to do this?
As already mentioned by Wiktor in the comments, the easiest regular expression is
^[^.]+
The explanation from regex101.com is:
^ asserts position at start of a line
Match a single character not present in the list below [^.]+
+ Quantifier — Matches between one and unlimited times, as many times as
possible, giving back as needed (greedy)
. matches the character . literally (case sensitive)
If you are using a programming language, another possible solution is to split the string in the dot character and get the first element of the resulting array. For example:
const array1 = 'server1.mydomain.com'.split(/\./);
console.log(array1[0]);
const array2 = 'desktop-1.mydomain.com'.split(/\./);
console.log(array2[0]);
Prints:
server1
desktop-1

Regex to match given amount of characters in undefined order [duplicate]

This question already has answers here:
Regex to match exactly n occurrences of letters and m occurrences of digits
(3 answers)
Closed 4 years ago.
I am looking for a regex that matches the following:
2 times the character 'a' and 3 times the character 'b'.
Additionally, the characters do not have to be subsequent, meaning that not only 'aabbb' and 'bbaaa' should be allowed, but also 'ababb', 'abbab' and so forth.
By the sound of it this should be an easy task, but atm I just can't wrap my head around it. Redirection to a good read is appreciated.
You need to use positive lookaheads. This is the same as the password validation problem described here.
Edit:
A positive lookahed will allow you to check a pattern against the string without changing where the next part of the regex matches. This means that you can test multiple regex patterns at the current position of the string and for the regex to match all the positive lookaheads will have to match.
In your case you are looking for 2 a' and 3 b's so the regex to match exactly 2 a's anywhere in the string is /^[^a]*a[^a]*a[^a]*$/ and for 3 b's is /^[^b]*b[^b]*b[^b]*b[^b]*$/ we now need to combine these so that we can match both together as follows /^(?=[^a]*a[^a]*a[^a]*$)(?=[^b]*b[^b]*b[^b]*b[^b]*$).*$/. This will start at the beginning of the string with the ^ anchor, then look for exactly 2 a's then the end of the string. Then because that was a positive lookahead the (?= ... ) the position for the next part of the pattern to match at in the string wont move so we are still at the start of the string and now match exactly 3 b's. As this is a positive lookahead we are still at the beginning of the string but now know that we have 2 a's and 3'b in the string so we match the whole of the string with .*$.