I need to match a string that can be of length from 1 to 20 characters maximum, and it contains letters a-g and numbers 1-7. However, the numbers cannot be next to each other - only single digit numbers are allowed.
Valid strings: aabbca1a6, 4gg1g2g1, 1
Invalid string: aabbca16a - theres two numbers next to each other, forming a two digit number 16.
I can match most strings quite easily with [a-g1-7]{1,20}, however i have no idea how to detect when two numbers are next to each other efficiently.
Currently in my program, after parsing through the regex, i'm just going through the whole string again in a loop, making sure there's no 2 numbers next to each other, however i'd prefer if it all could be done with just one (simple) regex.
You can use the answer from the comments by Ulugbek Umirov using negative lookahead at the start of an anchored string asserting not 2 digits to the right.
^(?!.*\d{2})[a-g1-7]{1,20}$
The pattern matches:
^ Start of string
(?!.*\d{2}) Negative lookahead, assert not 2 digits
[a-g1-7]{1,20} Repeat the ranges in the character class 1-20 times
$ End of string
Regex demo
Another option could be asserting the string length and repeat matching in a way that there can not be 2 digits next to each other
^(?=[a-g1-7]{1,20}$)[a-g]*(?:[1-7][a-g]+)*[0-7]?$
Regex demo
The simplest regex and approach is to check if it doesn't match:
.*\d\d.*
The best way to solve this problem is to use this trick
-Check if number between 1-7 and character between a-g and the numbers are not siblings with each other by using this pattern
^[1-7]?([a-g]+[1-7]?)*
-Then you can check the length of string using string methods like (length method in JavaScript)
Related
I should only catch numbers which are fit the rules.
Rules:
it should be 16 digit
first 11 digit can be any number
after 3 digit should have all zero
last two digit can be any number.
I did this way;
([0-9]{11}[0]{3}[0-9]{2})
number example:
1234567890100012
now I want to get the number even it has got any letter beginning or ending of the string like " abc1234567890100012abc"
my output should be just number like "1234567890100012"
When I add [a-zA-Z]* it gives all string.
Also another point is if there is any number beginning or ending of the string like "999912345678901000129999". program shouldn't take this. I mean It should return none or nothing. How can I write this with regex.
You can use look around to exclude the cases where there are more digits before/after:
(?<!\d)\d{11}000\d\d(?!\d)
On regex101
You can use a capture group, and match optional chars a-zA-Z before and after the group.
To prevent a partial match, you can use word boundaries \b or if the string should match from the start and end of the line you can use anchors ^ and $
\b[a-zA-Z]*([0-9]{11}000[0-9]{2})[a-zA-Z]*\b
Regex demo
I'm trying to use RegEx to create an input mask. The first letter can be either A or B and it has to be 5 digits after the number but ranging from 1-99999.
For example,
A00001
B20000
B00412
This is what I have so far,
^[S|T]{1}[0-9]{4}[1-9]
but it's not allowing A52210 for example.
Thanks in advance :)
Brief
I'm not sure what [S|T] is supposed to do, but that is saying one of S|T - one of S, |, or T. Also the {1} is irrelevant and your numbers won't work for the range you expect ([0-9]{4}[1-9] says 00001 to 99999, but not any number followed by a zero, i.e. 11110).
Code
See regex in use here
^[AB](?!0{5})\d{5}$
Note: If this regex is to be used on a long string and the contents of the string might include the string you're searching for, you should replace both position assertions ^ and $ with a word boundary \b as per the following regex.
\b[AB](?!0{5})\d{5}\b
Variations
Slightly shorter version, but dependent on end of string (won't work if the string is in the middle of a sentence)
^[AB](?!0+$)\d{5}$
Negative lookbehind (won't work in some flavours of regex)
^[AB]\d{5}(?<!0{5})$
Long nuisance regex that checks every possibility, but ensures that at least one number is not 0
^AB$
Results
Input
A00001
B20000
B00412
A52210
A00001
A00000
S00001
A1000
Output
Note: Below are matches; anything from Input above that isn't here wasn't matched.
A00001
B20000
B00412
A52210
A00001
Explanation
^ Assert position at the start of the line
[AB] Match a character from the set (either A or B literally)
(?!0{5}) Ensure what follows isn't 0 five times
\d{5} Match any digit five times
$ Assert position at the end of the line
I want to validate number that don't contain the minus char. The number > 0.
Have you got a regex for that ?
Exclusivly non-negativ numbers with decimal-point: ^\d+(?:.\d+)?$, or capturing with negativ look-behind ((?<!-)[[:digit:]]+) or a myriad other ways depending on the flavour of regex you need and the real problem at hand.
To match absolutes
^\d+$
https://regex101.com/r/O4nGl5/2
To match decimals
^\d+(\.?\d+)?$
https://regex101.com/r/O4nGl5/3
There multiple ways to do that, one of them is:
^[0-9]+$ (for integer numbers)
It checks your input against:
Starts and ends with an integer (^ for beginning and $ for end)
characters between between 0 and 9 (integers)
1 or more occurence (+ for 1 or more occurrences of the previous expression)
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})$
Can I use
\d\d\d\d[^\d]
to check for four consecutive numbers?
For example,
411112 OK
455553 OK
1200003 OK
f44443 OK
g55553 OK
3333 OK
f4442 No
45553 No
f4444g4444 No
f44444444 No
If you want to find any series of 4 digits in a string /\d\d\d\d/ or /\d{4}/ will do. If you want to find a series of exactly 4 digits, use /[^\d]\d{4}[^\d]/. If the string should simply contain 4 consecutive digits use /^\d{4}$/.
Edit: I think you want to find 4 of the same digits, you need a backreference for that. /(\d)\1{3}/ is probably what you're looking for.
Edit 2: /(^|(.)(?!\2))(\d)\3{3}(?!\3)/ will only match strings with exactly 4 of the same consecutive digits.
The first group matches the start of the string or any character. Then there's a negative look-ahead that uses the first group to ensure that the following characters don't match the first character, if any. The third group matches any digit, which is then repeated 3 times with a backreference to group 3. Finally there's a look-ahead that ensures that the following character doesn't match the series of consecutive digits.
This sort of stuff is difficult to do in javascript because you don't have things like forward references and look-behind.
Should the numbers be part of a string, or do you want only the four numbers. In the later case, the regexp should be ^\d{4}$. The ^ marks the beginning of the string, $ the end. That makes sure, that only four numbers are valid, and nothing before or after that.
That should match four digits (\d\d\d\d) followed by a non digit character ([^\d]). If you just want to match any four digits, you should used \d\d\d\d or \d{4}. If you want to make sure that the string contains just four consecutive digits, use ^\d{4}$. The ^ will instruct the regex engine to start matching at the beginning of the string while the $ will instruct the regex engine to stop matching at the end of the string.