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.
Related
I have searched around quite a bit, but I haven't found a solution for my problem yet.
I'm trying to create a regex that will allow me to match the following examples:
YOUU 410831 0
MEIU 810851 0
I got to \b(YOUU|MEIU)\w*\b.
But then I can't seem to add a space, then a number, then a space again, and finally a digit. How could I achieve this?
You are looking for something like
[A-Z]+(?:\s+[0-9]+)+
See demo
Or, if there are 2 set groups of numbers after the word, and the 1st number is 6 digits in size, and the last digit is always of size 1:
[A-Z]+\s+[0-9]{6}\s+[0-9]\b
Demo 2
With i option, the words with lowercase letters will also be matched.
Try to use this:
\b(YOUU|MEIU) \d+ \d\b
REGEX DEMO
You are probably looking for this regex?
\b(YOUU|MEIU)\s+\d+\b\s+\d
if the numbers in the middle are always 6 numbers, you may want to fix that with
\b(YOUU|MEIU)\s+\d{6}\b\s+\d
I need a regex which matches with 1 to 6 length digits which is easy \d{1,6}, however I need avoid if there is longer number than 6 digits.
For example, it should match 233 in sentence, my id is 233, but it should not match my id is 222334444
It should only match if there is space before first digit of number.
Thanks
Its hard to tell what the expected character is on either side.
Generally, it could be done using word boundary \b\d{1,6}\b
Use lookarounds.
(?<=^|\s)\d{1,6}(?!\d)
How about?
Online Demo
(?<!\d)\d{1,6}$
I'm real newbie when it comes to Regex so apologies if this 'should' be easy.
I need to match the last 6 digits of a number that has the following format
308950 3200 014559
The first 2 groups of numbers will remain constant (308950 3200) and don't need to be extracted. I am only interested in the last 6 digits.
The full number may contain spaces but these need to be optional.
This has to be done in Regex.
Use regex pattern
(?<=\b308950\s*3200\s*)\d{6}\b
or
\b308950\s*3200\s*(\d{6})\b
This should do it even if there are spaces between the digits
^308950 3200[\d\s]*?((\d\s?){6})$
Group 1 will contain the reqired digits with spaces if any
If the leading numbers will remain constant, you can use:
308950 3200\s*(\d{6})
Alternatively, you could use:
(?:\d+\s)+(\d{6})
Also, if the string will be at the end of the input string, consider adding a $ to the end to signify this (to make sure it'll match the end of the string):
(\d{6})$
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
I have two regular expressions that validate the values entered.
One that allows any length of Alpha-Numeric value:
#"^\s*(?<ALPHA>[A-Z0-9]+)\s*"
And the other only allows numerical values:
#"^\s*(?<NUM>[0-9]{10})"
How can I get a numerical string of the length of 11 not to be catched by the NUM regex.
I think what you're trying to say is that you don't want to allow any more than 10 digits. So, just add a $ at the end to specify the end of the regex.
Example: #"^\s*(?[0-9]{10})$"
Here's my original answer, but I think I read you too exact.
string myRegexString = `#"(?!(^\d{11}$)` ... your regex here ... )";
That reads "while ahead is not, start, 11 digits, end"
If it's single line, you could specify that your match must happen at the end of the line, like this in .net ...
^\s*([0-9]{10})\z
That will accept 1234567890 but reject 12345678901.
Do you mean you want to match up to 10 digits? Try this:
#"^\s*[0-9]{1,10}\s*$"
If you are trying to match only numbers that are 10 digits long, just add a trailing anchor using $, like this:
^\s*(?:[0-9]{10})\s*$
That will match any number that is exactly 10 digits long (with optional space on either side).
var pattern =/\b[0-9]{10}$\b/;
// the b modifier is used for boundary and $ is used for exact length
Match something non-numeric after the length 10 string. My regex-foo isn't that good, but I think you've got it setup there to catch a numeric string of exactly length 10, but since you don't match anything after that, a length 11 string would also match. Try matching beyond the end of the number and you'll be good.
This should match only 10 digits and allow arbitrary numbers of whitespaces before and after the digits.
Non-capturing version: (only matches, the matched digits are not stored)
^\s*(?:\d{10})\s*$
Capturing version: (the matched digits are available in subgroup 1, as $1 or \1)
^\s*(\d{10})\s*$
You could try alternation?
^\s*(?\d{1,10}|\d{12,})