Changing existing regex so at least 1 numerical value is required - regex

Said regex: \b(?=(?:[a-z\d]*[A-Z]){3})(?=.*\d)(?=(?:[A-Z\d]*[a-z]){2})[a-zA-Z\d]{5,30}\b
I am trying to add just 1 condition to this regex, that being that 1 number is required in order to match, I have tried inserting the lookahead (?=.*\d) but it did not work, as it matches the "HeLLoWoRlD" portion of <HeLLoWoRlD"123

You may add the (?=[A-Za-z]*\d) lookahead check:
\b(?=[A-Za-z]*\d)(?=(?:[a-z\d]*[A-Z]){3})(?=(?:[A-Z\d]*[a-z]){2})[a-zA-Z\d]{5,30}\b
^^^^^^^^^^^^^^^
See the regex demo.
The (?=[A-Za-z]*\d) lookahead matches a location that is immediately followed with 0 or more ASCII letters and then one digit.

Related

How to validate pattern AB12CD

The requirement is "each 2 digits must be only numbers or only text" - so valid patterns are AB-12-CD or 12-AB-CD or AB-CD-12, 12-34-AB.
The below suggested pattern working fine without hyphen but if we want to add a hyphen in between, how to do?
\b(?=[A-Z\d][A-Z])(?=[A-Z\d]\d)(?:[A-Z]{2}|\d{2})+\b
Repeat 1 or more times matching either 2 uppercase chars or 2 digits. Note that there are no hyphens present in the example data.
\b(?:[A-Z]{2}|\d{2})+\b
Regex demo
If there must be a digit and an uppercase character present, you could also use a positive lookahead:
\b(?=[A-Z\d]*[A-Z])(?=[A-Z\d]*\d)(?:[A-Z]{2}|\d{2})+\b
Regex demo

Regex match an entire number with lookbehind and look ahead logic(without word boundaries)

I am trying to detect if a string has a number based on few conditions. For example I don't want to match the number if it's surrounded by parentheses and I'm using the lookahead and lookbehind to do this but I'm running into issues when the number contains multiple digits. Also, the number can be between text without any space separators.
My regex:
(?https://regex101.com/r/RnTSMJ/1
Sample examples:
{2}: Should NOT Match. //My regex Works
{34: Should NOT Match. //My regex matches 4 in {34
45}: Should NOT Match. //My regex matches 4 in {45
{123}: Should NOT Match. //My regex matches 2 in {123}
I looked at Regex.Match whole words but this approach doesn't work for me. If I use word boundaries, the above cases work as expected but then cases like the below don't where numbers are surrounded with text. I also want to add some additional logic like don't match specific strings like 1st, 2nd, etc or #1, #2, etc
updated regex:
(?<!\[|\{|\(|#)(\b\d+\b)(?!\]|\}|\|st|nd|rd|th)
See here https://regex101.com/r/DhE3K4/4
123abd //should match 123
abc345 //should match 234
ab2123cd // should match 2123
Is this possible with pure regex or do I need something more comprehensive?
You could match 1 or more digits asserting what is on the left and right is not {, } or a digit or any of the other options to the right
(?<![{\d#])\d+(?![\d}]|st|nd|rd|th)
Explanation
(?<![{\d#]) Negative lookbehind, assert what is on the left is not {, # or a digit
\d+ Match 1+ digits
(?! Negative lookahead, assert what is on the right is not
[\d}]|st|nd|rd|th Match a digit, } or any of the alternatives
) Close lookahead
Regex demo
The following regex is giving the expected result.
(?<![#\d{])\d+(?!\w*(?:}|(?:st|th|rd|nd)\b))
Regex Link

Global regex with multiple matches, where the separator should be shared in several matches

First of all, sorry for the unclear title, it's hard to describe (and to find an existing solution for the same reason).
I use this regex in Javascript, to collect numbers in a string :
/(?:^|[^\d])([\d]+)(?:$|[^\d])/g
Executing it on "5358..2145" returns 2 matches, where the submatches are "5358" and "2145"
But if I use it on "5358.2145", I receive only 1 match : "5358"
So, I understand it so :
The first match is found ("5358.") so the point goes in the first match
What I want as second match is not preceded with start of string or the point because this point already belongs to the first match
How can I change my pattern to find all numbers separated with 1 non-number character ?
Use a negative lookahead at the end:
/(?:^|\D)(\d+)(?!\d)/g
See the regex demo
The pattern matches:
(?:^|\D) - either start of string (^) or any non-digit char (\D)
(\d+) - Group 1: one or more digits
(?!\d) - the negative lookahead failing the match if there is a digit immediately to the right of the current location.

Regular expression as optional with maximum limit of 1

I'm new to regex. Facing some issues while making one expression as optional and if it exists then it should not be repeated. In the below case I want %23 to be optional and if it occurs then it should not be repeated. But in below case it's working for optional but not for repeat case.
It's giving me true even if I put string as:
-113%23%2313113098A%2F--
Could someone suggest how to make it optional and not repetitive. This is my regex:
(%23)?([0-9]|[A-Z]|%2F|-).*$
You can use a negative lookahead to avoid matching repeating instances of %23:
^(?:[0-9]|[A-Z]|%2F|[-%])(?!(?:.*?%23){2}).*$
Breakup:
(?! # start negative lookahead
(?:.*?%23){2} # match 0 or more chars followed by %23, {2} matches 2 repeats
) # end lookahead
RegEx Demo
However if requirement is to avoid consecutive repeats then use:
^(?!.*?(?:%23){2})

A special Regular Expression

I want to have a restriction a string which can accept alphanumeric values and hiphen.
I am providing 3 examples to have a clear idea.
1) AS15JKM-125TR-325AMOR
2) ITEW32-DE432OI
3) 09IURE765EDR
There is no specific pattern, There may b 0 to 3 hiphens in a string.
I just want to restrict it in such a way that it should accept only alphanumeric value and
only Hiphen, no other special character.
plz help me on this.
Option 1: No Lookahead
^(?:[A-Za-z0-9]*-){0,3}[A-Za-z0-9]+$
Note that if you only want uppercase letters, you need to remove a-z
Explanation
The ^ anchor asserts that we are at the beginning of the string
The non-capturing group (?:[A-Za-z0-9]*-) matches zero or more letters or digit, then a hyphen
This is repeated zero to three times, enforcing your limit on hyphens
[A-Za-z0-9]+ matches one or more letters or digit
The $ anchor asserts that we are at the end of the string
Option 2: With Lookahead
This does not present any benefit over the first version, I am just showing it for completion.
^(?=(?:[^-]*-){0,3}[^-]*$)[A-Za-z0-9]+$
Explanation
The lookahead (?=(?:[^-]*-){0,3}[^-]*$) asserts that what follows is
(?:[^-]*-) any number of non-hyphens, followed by a hyphen
{0,3} zero to three times
then [^-]*$ any number of non-hyphens and the end of the string
Option 3: With Negative Lookahead
Courtesy of #Jerry:
^(?!(?:[^-]*-){4})[A-Za-z0-9]+$
Explanation
The negative lookahead (?!(?:[^-]*-){4}) asserts that it is not possible to find a non-hyphen followed by a hyphen four times.
Assuming you do not want to count the hyphens, something like so should work: ^[A-Z0-9 -]+$.
An example of the regex is available here.