Regular Expression for testing length of string part - regex

Hi I need a regular expression which is accepting the following strings:
[A-Z]-[A-Z]{3-5}[0-9]{2-4}
for example X-ABC123 or Y-AB1234
The problem now is that the total length of the string on the right side of the hyphen must always be 5 chars in length. Is there a chance to check that with regular expressions only?

Just add this after the hyphen :
/(?=[A-Z\d]{5}$)/
Resulting in :
/^[A-Z]-(?=[A-Z\d]{5}$)[A-Z]{3,5}[0-9]{2,4}/
This assumes that your input strings are the strings you posted.
X-ABC123 -> fails
Y-AB1234 -> fails
A-ABD12 -> matches
A-ABV111 -> fails
If the string is part of another string you can replace the $ anchor with \s|$ for example.

First the problems in your regex
The quantifier is {3,5} and not {3-5} (this would match literally "{3-5}")
You want 3 to 5 letters and 2 to 4 digits and in total 5 letters and digits ==> the only valid combination is then 3 letters followed by 2 digits.
In general you can use a positive lookahead for this
^[A-Z]-(?=.{5}$)[A-Z]{3,5}[0-9]{2,4}$
See it here on Regexr
The (?=.{5}$) part is just looking, if there are from its position to the end ($) 5 characters.
But as said before, if the 3-5 and 2-4 and overall 5 is valid you can just do
^[A-Z]-[A-Z]{3}[0-9]{2}$

I think it's definitely possible, from a language theory point of view. Just group it and add the constraint :
I just need to know which language is specifying the regex but something like this :
[A-Z]-(^[A-Z][A-Z0-9]4)
I had the feeling you wanted the right part to start with one char for sure, then either chars or numbers

Related

Regex with 3 characters long and at least one letter

I wondering if it is possible to have this test in one regex
Length at least 3 and at least one letter
I test regex like this
([a-zA-Z]{1,}).(\w{3,})
but not working
sample :
Z56 => OK
1567 => KO
EE => KO
thx you
78 I => OK
What you are looking for as I understand is this one:
^(?:([a-zA-Z]{1})(\w{0,2})|((?:\s*\w){0,3}))$
I added ^ and $ to specify that the string must start and end, and The second part {0,2} to set the length between 0 and 2, you can change the first part {1} with ? to accept empty strings, and the alternative | to capture the last example 78 I
If you want to capture the characters then:
^(?=.*[A-Za-z])\w{3,}$
See Regex Demo
^ Matches the start of the string.
(?=.*[A-Za-z]) Is a positive lookahead assertion requiring that the input contains a letter.
\w{3,} Matches 3 or more word characters (so the input must be length 3 or longer).
$ Matches the end of the string.
Use the following regex if the input can be 3 or more characters of any type as long as there is at least one letter (there is a discrepancy between the the question title, which just states 3 characters and the regex you attempted), so it is not entirely clear which one is intended:
^(?=.*[A-Za-z]).{3,}$
(?=.*[A-Za-z]).{3,}
Should work.

Reg Exp: match specific number of characters or digits

My RegExp is very rusty! I have two questions, related to the following RegExp
Question Part 1
I'm trying to get the following RegExp to work
^.*\d{1}\.{1}\d{1}[A-Z]{5}.*$
What I'm trying to pass is x1.1SMITHx or x1.1.JONESx
Where x can be anything of any length but the SMITH or JONES part of the input string is checked for 5 upper case characters only
So:
some preamble 1.1SMITH some more characters 123
xyz1.1JONES some more characters 123
both pass
But
another bit of string1.1SMITHABC some more characters 123
xyz1.1ME some more characters 123
Should not pass because SMITH now contains 3 additional characters, ABC, and ME is only 2 characters.
I only pass if after 1.1 there are 5 characters only
Question Part 2
How do I match on specific number of digits ?
Not bothered what they are, it's the number of them that I can't get working
if I use ^\d{1}$ I'd have thought it'll only pass if one digit is present
It will pass 5 but it also passes 67
It should fail 67 as it's two digits in length.
The RegExp should pass only if 1 digit is present.
For the first one, check out this regex:
^.*\d\.\d[A-Z]{5}[^A-Z]*$
Before solving the problem, I made it easier to read by removing all of the {1}. This is an unnecessary qualifier since regex will default to looking for one character (/abc/ matches abc not aaabbbccc).
To fix the issue, we just need to replace your final .*. This says match 0+ characters of anything. If we make this "dot-match-all" more specific (i.e. [^A-Z]), you won't match SMITHABC.
I came up with a number of solution but I like these most. If your RegEx engine supports negative look-ahead and negative look-behind, you can use this:
Part 1: (?<![A-Z])[A-Z]{5}(?![A-Z])
Part 2: (?<!\d)\d(?!\d)
Both have a pattern of (?<!expr)expr(?!expr).
(?<!...) is a negative look-behind, meaning the match isn't preceded by the expression in the bracket.
(?!...) is a negative look-ahead, meaning the match isn't followed by the expression in the bracket.
So: for the first pattern, it means "find 5 uppercase characters that are neither preceded nor followed by another uppercase character". In other words, match exactly 5 uppercase characters.
The second pattern works the same way: find a digit that is not preceded or followed by another digit.
You can try it on Regex 101.

Regex of exact length that matches specific group of numbers

I'm looking to build a regex that matches the following group of numbers:
10xxxxxxx
1116xxxxx
143xxxxxx
146xxxxxx
149xxxxxx
159xxxxxx
16xxxxxxx
(note the length is always 9)
where x is any digit. My best attempt yielded this:
/^1[01456][1369]*[6]*[0-9]$/
However, I can't get the length of the string to always be 9. Any ideas?
Edit: Maybe I wasn't clear enough, it needs to match those 7 cases, and ONLY those, inclusively and exclusively.
How about:
^1(?:[06]\d{2}|116|4[369]\d|59\d)\d{5}$
use this pattern
^1[01456](16|3\d|6\d|9\d|\d\d)\d{5}$
Is this what you want?
^(?=[0-9]{9}$)(?:10|1116|143|146|149|159|16)
Demo
This starts by looking at the beginning of the string for exactly 9 digits using a positive lookahead anchored to the end of the string. Then we look for any of your 7 specific groups of numbers that the string can start with.
You can use this regex:
/^1[01456][1369][0-9]{6}$/
Since 3 digits are already matched by first 3 patterns 1, [01456] and [1369] so last one must match exact 6 characters to enforce it a 9 digit input.

Limit length of string containing at least 1 digits , 0 or more characters and optional dash

I am trying to make a regular expression for consumer products models.
I have this regular expression: ([a-z]*-?[0-9]+-?[a-z]*-?){4,}
which I expect to limit this whole special string to 4 or more but what happens is that the limit is applied to only the digits.
So this example matches: E1912H while this does not: EM24A1BF although both should match.
Can you tell me what I am doing wrong or how can I make the limit to the whole special string not only the digits?
Limitations:
1- String contains at least 1 digit
2- string can contains characters
3- string can contain "-"
4- minimum length = 4
Summary of your conditions so far:
require at least 1 digit [0-9]
require at least 4 symbols {4,}
can have characters [a-zA-Z]
can have short dash [-]
The following regexp meets them all:
^(?=.*\d)([A-Za-z0-9-]+){4,}$
Note: ^ and $ symbols mean entire input string is validated. Alter this if it`s not the case.
it cant match... EM24A1BF contains EM, which are 2 [a-z], not 1 as your regex states.
Something like this
[a-z]*-?\d+-?[a-z]*-?\d*[a-z]+
matches both your expression and all these:
E1912H
EM24A1BF
eM24A1BF
eM-24A-1BF
eM-24A-
eM24A-1BF
eM-24A1BF
To be sure your string meets both your requirements (the characters'position and composition AND the length requirement), you need to use a non-consuming regular expression
Check this out
([\w-]*\d+[\w-]*){4,}
it matches the following
32ES5200G
LE32K900
N55XT770XWAU3D

Limit number of alpha characters in regular expression

I've been struggling to figure out how to best do this regular expression.
Here are my requirements:
Up to 8 characters
Can only be alphanumeric
Can only contain up to three alpha characters [a-z] (zero alpha characters are valid to)
Any ideas would be appreciated.
This is what I've got so far, but it only looks for contiguous letter characters:
^(\d|([A-Za-z])(?!([A-Za-z]{3,}))){0,8}$
I'd write it like this:
^(?=[a-z0-9]{0,8}$)(?:\d*[a-z]){0,3}\d*$
It has two parts:
(?=[a-z0-9]{0,8}$)
Looksahead and matches up to 8 alphanumeric to the end of the string
(?:\d*[a-z]){0,3}\d*$
Essentially allowing injection of up to 3 [a-z] among \d*
Rubular
On rubular.com
12345678 // matches
123456789
#(#*#$
12345 // matches
abc12345
abcd1234
12a34b5c // matches
12ab34cd
123a456 // matches
Alternatives
I do think regex is the best solution for this, but since the string is short, it would be a lot more readable to do this in two steps as follows:
It must match [a-z0-9]{0,8}
Then, delete all \d
The length must now be <= 3
Do you have to do this in exactly one regular expression? It is possible to do that with standard regular expressions, but the regular expression will be rather long and complicated. You can do better with some of the Perl extensions, but depending on what language you're using, they may or may not be supported. The cleanest solution is probably to check whether the string matches:
^[A-Za-z0-9]{0,8}$
but doesn't match:
([A-Za-z].*){4}
i.e. it's an alpha string of up to 8 characters (first regular expression), but doesn't contain 4 or more alpha characters (possibly separated by other characters (second regular expression).
/^(?!(?:\d*[a-z]){4})[a-z0-9]{0,8}$/i
Explanation:
[a-z0-9]{0,8} matches up to 8 alphanumerics.
Lookahead should be placed before the matching happens.
The (?:\d*[a-z]) matches 1 alphabetic anywhere. The {4} make the count to 4. So this disables the regex from matching when 4 alphabetics can be found (i.e. limit the count to ≤3).
It's better not to exploit regex like this. Suppose you use this solution, are you sure you will know what the code is doing when you revisit it 1 year later? A clearer way is just check rule-by-rule, e.g.
if len(theText) <= 8 and theText.isalnum():
if sum(1 for c in theText if c.isalpha()) <= 3:
# valid
The easiest way to do this would be in multiple steps:
Test the string against /^[a-z0-9]{0,8}$/i -- the string is up to 8 characters and only alphanumeric
Make a copy of the string, delete all non-alphabetic characters
See if the resulting string has a length of 3 or less.
If you want to do it in one regular expression, you can use something like:
/^(?=\d*(?:[a-z]?\d*){0,3}$)[a-z0-9]{0,8}$/i
Which looks for a alphanumeric string between length 0 and 8 (^[a-z0-9]{0,8}$), but first uses a lookahead ((?=\d*(?:[a-z]?\d*){0,3}$)) to make sure that the string
has at most 3 alphabetic characters.