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
Related
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)
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 want to find a regex binary string expression that has at least one 1 and an even number of 0.
For strings that has an even number of 0, I have:
1*(01*01*)*
For strings with at least one 1, I have:
0*1(0+1)*
However, I struggle to combine the two together. Can anyone give me a hint on how to do it? thanks!
Without any fancy regex stuff, just ^ start, $ end anchors and | pipe, how about
^(00)*(1|01+0)(1*01*0)*1*$
Should be self explanatory. Play with it at regex101. Hope I've not overlooked anything :)
If lookarounds are allowed, I could also think of a negative one: ^(?!0*$)(?:1*01*0)*1*$
I'm not sure if I understand your requirements correctly, but if + should stand for alternation (normally |), and only *, +, and brackets are allowed (as your comment below suggests) then
(1*01*01*)*(11*+011*0)(1*01*01*)*
should work.
The idea is that there must exist a 1 such that either left and right from it must be an equal number of 0s or left and right from it must be an odd number of 0s: (11*+011*0)
Your 0*1(0+1)* would also match 01 so I believe it doesn't work.
In either case left and right from such a group there can exist an equal number of 0s with an arbitrary number 1s in between: (1*01*01*)*.
In most regex implementations the above regex would be written as
^(1*01*01*)*(11*|011*0)(1*01*01*)*$.
One option to match an even number of zeroes and at least a single 1 might be using a positive lookahead (?= (if supported)
^(?=(?:1*01*01*)+$)0*1[10]*$
^ Start of string
(?= Positive lookahead, assert what is on the right is
(?:1*01*01*)+$ Repeat 1+ times matching two times a zero with optional one's
) Close lookahead
0*1 Match zere or more times a zero, then match the required one
[10]* Match 0+ times a zero or one
$ End of string
Regex demo
To also match only one's (so without a zero) you could use
^(?=(?:(?:1*01*01*)+|1+)$)0*1[10]*$
Regex demo
First off, this has sort of been asked before. However I haven't been able to modify this to fit my requirement.
In short: I want a regex that matches an expression if and only if it only contains digits, and there are 5 (or more) increasing consecutive digits somewhere in the expression.
I understand the logic of
^(?=\d{5}$)1*2*3*4*5*6*7*8*9*0*$
however, this limits the expression to 5 digits. I want there to be able to be digits before and after the expression. So 1111345671111 should match, while 11111 shouldn't.
I thought this might work:
^[0-9]*(?=\d{5}0*1*2*3*4*5*6*7*8*9*)[0-9]*$
which I interpret as:
^$: The entire expression must only contain what's between these 2 symbols
[0-9]*: Any digits between 0-9, 0 or more times followed by:
(?=\d{5}0*1*2*3*4*5*6*7*8*9*): A part where at least 5 increasing digits are found followed by:
[0-9]*: Any digits between 0-9, 0 or more times.
However this regex is incorrect, as for example 11111 matches. How can I solve this problem using a regex? So examples of expressions to match:
00001459000
12345
This shouldn't match:
abc12345
9871234444
While this problem can be solved using pure regular expressions (the set of strictly ascending five-digit strings is finite, so you could just enumerate all of them), it's not a good fit for regexes.
That said, here's how I'd do it if I had to:
^\d*(?=\d{5}(\d*)$)0?1?2?3?4?5?6?7?8?9?\1$
Core idea: 0?1?2?3?4?5?6?7?8?9? matches an ascending numeric substring, but it doesn't restrict its length. Every single part is optional, so it can match anything from "" (empty string) to the full "0123456789".
We can force it to match exactly 5 characters by combining a look-ahead of five digits and an arbitrary suffix (which we capture) and a backreference \1 (which must exactly the suffix matched by the look-ahead, ensuring we've now walked ahead 5 characters in the string).
Live demo: https://regex101.com/r/03rJET/3
(By the way, your explanation of (?=\d{5}0*1*2*3*4*5*6*7*8*9*) is incorrect: It looks ahead to match exactly 5 digits, followed by 0 or more occurrences of 0, followed by 0 or more occurrences of 1, etc.)
Because the starting position of the increasing digits isn't known in advance, and the consecutive increasing digits don't end at the end of the string, the linked answer's concise pattern won't work here. I don't think this is possible without being repetitive; alternate between all possibilities of increasing digits. A 0 must be followed by [1-9]. (0(?=[1-9])) A 1 must be followed by [2-9]. A 2 must be followed by [3-9], and so on. Alternate between these possibilities in a group, and repeat that group four times, and then match any digit after that (the lookahead in the last repeated digit in the previous group will ensure that this 5th digit is in sequence as well).
First lookahead for digits followed by the end of the string, then match the alternations described above, followed by one or more digits:
^(?=\d+$)\d*?(?:0(?=[1-9])|1(?=[2-9])|2(?=[3-9])|3(?=[4-9])|4(?=[5-9])|5(?=[6-9])|6(?=[7-9])|7(?=[89])|8(?=9)){4}\d+
Separated out for better readability:
^(?=\d+$)\d*?
(?:
0(?=[1-9])|
1(?=[2-9])|
2(?=[3-9])|
3(?=[4-9])|
4(?=[5-9])|
5(?=[6-9])|
6(?=[7-9])|
7(?=[89])|
8(?=9)
){4}
\d+
The lazy quantifier in the first line there \d*? isn't necessary, but it makes the pattern a bit more efficient (otherwise it initially greedily matches the whole string, requiring lots of failing alternations and backtracking until at least 5 characters before the end of the string)
https://regex101.com/r/03rJET/2
It's ugly, but it works.
Recently I am thinking the reason why we need a * in regular expression. For example, if we want to represent A0,A1..,Z99, we can do:
[A-Z][0-9][0-9]*
But A0A (which is not we want) is also valid according to the above. What benefit does the * give me?
* is just a quantifier, matching between zero and unlimited times.
[A-Z][0-9][0-9]* matches A0,A1..,Z99 and also A10000,Z123456789...
Remembering that if you dont put the ^ and $ as anchors, the processor will match the specified part, and return true even if the input contain more characters, because you don't said that you want a positive result ONLY if the entire input matches the regex.
If your goal is to match just A0,A1..,Z99, the regex should be:
^[A-Z][0-9][0-9]?$
Or simply:
^[A-Z]\d{1,2}$
\d means 'digit', and is the same as [0-9].
{1,2} means at least 1 time and nothing more than 2 times.
? also is a quantifier, matching 0 or 1 time.
But A0A (which is not we want) is also valid
No it is not valid, you just need to use anchors:
^[A-Z][0-9][0-9]*$
^ will ensure this matches at line start and $ ensures it matches till line end.
Also if only 2nd digit is optional then better to use:
^[A-Z][0-9][0-9]?$
Since * matches 0 or more times whereas ? matches 0 or 1 time.
Seems like you're trying to match the strings starts with an uppercase alphabet and the following numbers ranges from 1 to 99.
^[A-Z][1-9]?[0-9]$
^ asserts that we are at the start and $ asserts that we are at the end. So this helps to do an exact string match. It won't match at the middle or start or at the end of a string or line. That is, [A-Z][1-9]?[0-9] will match A10 in fooA10 string but ^[A-Z][1-9]?[0-9]$ won't produce a match in fooA10 string.