Regex of dynamic block of characters - regex

I have a block of number characters separated by '=' and the number range can be always different on both sides.
Example: 12345678999999=654784651321, next time it could be: 4567894135=456789211
I need help with finding suitable regex which select me always numbers between first 6 and last 4 digits of left side of block and then all numbers after 7th digit of right side of block:
123456[][][][]9999=6547846[][][][][]
Is this somehow possible?

[0-9]{6}([0-9]*)[0-9]{4}=[0-9]{7}([0-9]*)

Assuming the difficulty isn't matching a continuous set of digits, but rather matches each digit seperately try:
(?:^\d{6}|=\d{7}|\G)(?=\d{5,8}=|\d*$)\K\d
See an online demo
(?: - Open non-capture group for alternation;
^\d{6} - Match start-line anchor followed by 6 digits;
| - Or;
=\d{7} - Match a literal '=' followed by exactly 7 digits;
| - Or;
\G - Assert position at end of previous match or start of string;
(?=\d{5,8}=|\d*$) - Positive lookahead to assert possition is followed by either 5-8 digits upto an '=' or 0+ (greedy) digits upto end-line anchor;
\K - Reset starting point of previous reported match;
\d - A single digit.
Alternatively, if you have an environment that supports zero-width lookbehind like JavaScript or PyPi's regex package in Python, try:
(?:(?=\d{5,8}=)(?<=\d{6})|(?<==\d{7,}))\d
See an online demo
(?: - Open non-capture group for alternation;
(?=\d{5,8}=)(?<=\d{6}) - Positive lookahead to assert position is followed by 5-8 digits and an '=' but also preceded by at least 6 digits;
| - Or;
(?<==\d{7,}) - Positive lookbehind to assert position is preceded by an '=' followed by 7+ digits;
\d - A single digit.

// 6 NOT number or =
// ||
// \/
/[0-9]{6}[^\=0-9]*\=[^\=0-9]*[0-9]{4}/

Related

regex 13 digits phone number with dash

I have an application that needs to handle validation for phone numbers. Phone numbers are required to have 13 characters (sum of numbers and dashes). There must be at least 1 dash and a maximum of 3 dashes. The starting character must be a digit. How can I create a regex for this validation? Here is my regex string. /^(?:[0-9]-*){13}$/ It doesn't work exactly what I expected
So 13 characters in total with a maximum of 3 dashes and a minimum of 1 means 10 digits right? Therefor your characters are ranging 11-13?
If so, try:
^(?=(?:\d-?){10}$)\d+(?:-\d+){1,3}
See an online demo
^ - Start line anchor.
(?= - Open a positive lookahead:
(?: - Open a non-capture group:
\d-? - Match a digit and optional hyphen.
){10}$) - Close the non-capture group and match it ten times before the end-string anchor. Then close the lookahead.
\d+ - 1+ Digits.
(?: - Open a 2nd non-capture group:
-\d+ - Match an hyphen and 1+ digits.
){1,3} - Close non-capture group and match it 1-3 times.
You can use
^(?=.{13}$)[0-9]+(?:-[0-9]+){1,3}$
^(?=.{13}$)\d+(?:-\d+){1,3}$
See the regex demo. Details:
^ - start of string
(?=.{13}$) - the string must contain exactly 13 chars
[0-9]+ / \d+ - one or more digits
(?:-[0-9]+){1,3} / (?:-\d+){1,3} - one, two or three repetitions of a hyphen followed with one or more digits
$ - end of string.
See the regex graph:
A JavaScript demo:
const texts = ['123-4567-8901','123-45-67-890','123-456728901','1234567890123','123--67890123','-234567890123','123456789012-','-23456789012-'];
const regex = /^(?=.{13}$)\d+(?:-\d+){1,3}$/;
for (const text of texts) {
console.log(text, "=>", regex.test(text));
}
I assumed the number of digits are not known. I am looking for number of dashes between 1 and 3 between the numbers
texts = ['1-2-3-4-5','123-4567-8901','123-45-67-890','123-456728901','1234567890123','123--67890123','-234567890123','123456789012-','-23456789012-']
for text in texts:
print(text,re.findall(r"^\d+-?\d+-?\d+-?\d+$",text))
output:
1-2-3-4 ['1-2-3-4']
123-4567-8901 ['123-4567-8901']
123-45-67-890 ['123-45-67-890']
123-456728901 ['123-456728901']
1234567890123 ['1234567890123']
123--67890123 []
-234567890123 []
123456789012- []
-23456789012- []

Regex for numeric value that could contain comma & dots

I have been trying but without success
I need a regular expression for validating numbers that could contain dots and commas,
the number should be positive and there should be max of two numbers after the comma
Valid cases would be:
1000 - valid
1,000 - valid
1,000.22 - valid
-2 not valid
1,000.233 not valid
0 not valid
1.00,22 - not valid
Language is javascript
let valid =["1000","1,000","1,000.22"];
let notValid = ["-2","1,000.233 ","0","1.00,22"];
let rge = /^[1-9]+\d*(,\d{3})*(\.\d{1,2})?$/;
for(let x of valid)
console.log(x," è valida? ",rge.test(x));
for(let x of notValid)
console.log(x," è valida? ",rge.test(x));
Above there is a possible solution in Javascript, you haven't specified the language.
\d are numbers in the range [0-9]
The full stop . is a metacharacter (it means any character), to refer to the character . you have to escape it thereby \.
+ means at least 1 or more times
* means 0 or more times
? means 0 or 1 time
{1,2} means match minimum 1 time, maximum 2 times
The starting ^ and final $ refer to a exact matching otherwise you could have a partial matching of the string
A few assumptions:
Invalid: '123456789.12' and '12345,123.12'
I think the following does what you are after:
^[1-9](?:\d*|\d{0,2}(?:,\d{3})*(?:\.\d\d?)?)$
See the online demo
^ - Start-line anchor.
[1-9] - A single digit in the range 1-9.
(?: - Open a non-capture group:
\d* - 0+ Digits to allow any integer.
| - Or:
\d{0,2} - Between 0 to 2 digits;
(?:,\d{3})* - Followed by a non-capture group to allow any 0+ times a comma followed by 3 digits.
(?:\.\d\d?)? - Followed by an optional non-capture group to allow up to two decimals.
)$ - Close non-capture group and match the end-line anchor.
Or, if you also want to allow any integer followed by decimals (e.g: '123456789.01') you may change this to:
^[1-9](?:\d*|\d{0,2}(?:,\d{3})*)(?:\.\d\d?)?$
I think this regex should do the trick:
[1-9][\d,]*(\.\d{1,2})?
[1-9] - matches one character between 1 and 9 at the beginning (required to not match 0)
[\d,]* - matches zero or more digits or commas
(\.\d{1,2})? - zero or one group of a dot and one or two digits
For testing regexes I do recommend https://regex101.com/

Regex to match a digit pattern with rotations

What I'd like to match is the pattern 012345678901234567890123456789 but also 345678901234567890123456789012. Meaning, I want to match the first 30 characters of this pattern but I don't know in advance the first digit.
Is there a way to match any rotation of this pattern?
This could be done using alternation, however I'm not sure how efficient this is performance-wise:
^(?!.*(?:0[^1]|1[^2]|2[^3]|3[^4]|4[^5]|5[^6]|6[^7]|7[^8]|8[^9]|9[^0]))\d{30}$
See the online demo, where:
^ - Start string anchor.
(?! - Open a negative lookahead:
.* - 0+ characters other than newline.
(?: - Open non-capture group:
0[^1]|1[^2]|2[^3]|3[^4]|4[^5]|5[^6]|6[^7]|7[^8]|8[^9]|9[^0] - Basically assure that all digits are only followed by their appropriate successor using alternation.
) - Close non-capture group.
) - Close lookahead.
\d{30} - 30 Digits.
$ - End string anchor.
To ensure also the last digit within any group of characters and more positive (no negative lookahead but a positive one):
edit
to accommodate any sequence of the request it is actually better to
search for 29 digits followed by the right one
followed by one digit preceded by the right one:
line break added for readability only!
(0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)|9(?=0)){29}
((?<=0)1|(?<=1)2|(?<=2)3|(?<=3)4|(?<=4)5|(?<=5)6|(?<=6)7|(?<=7)8|(?<=8)9|(?<=9)0)
This looks quite perfect now to me :)
first solution failed on 31 digits in I a row
(0(?=1|[^\d])|1(?=2|[^\d])|2(?=3|[^\d])|3(?=4|[^\d])|4(?=5|[^\d])|5(?=6|[^\d])|6(?=7|[^\d])|7(?=8|[^\d])|8(?=9|[^\d])|9(?=0|[^\d])){30}
this searches for 30 numbers 0 to 9 followed by their successor or not a number:
https://regex101.com/r/rTYByt/1

Regex for a permutation of exactly 7 digits and 2 hyphens, without 2 consecutive hyphens

I a looking for a Regex to match a string which should:
start with a digit
'in-between' have a permutation of exactly 7 digits and 2 hyphens, without 2 consecutive hyphens
end with a sequence of digit, hyphen, digit
Match:
01-234-5678-9
01234-56-78-9
0123-4-5678-9
012-345-678-9
01-234567-8-9
01-234-5678-9
0-12345-678-9
0-123-45678-9
0-123-45678-9
01-23456-78-9
0-123456-78-9
0-1234567-8-9
No Match:
01-234-56789-0
01-234-567-8
01--2345678-9
01-2345678--9
0-1-23456789
-01-2345678-9
For now, I could not quite figure out how to match the 2 'in-between' hyphens: ^\d\d{7}\d-\d$
EDIT:
Thanks to the answers I had to this question, I was able to expand it to this other question regarding ISBN-10 and ISBN-13...
You can assert 7 digits and the digit - digit part at the end.
For the match there should be at least a single digit before and after the hyphen to prevent consecutive hyphens.
^\d(?=(?:-?\d){7}-?\d-\d$)\d*-\d+-\d*\d-\d$
^ Start of string
\d Match a single digit
(?= Positive lookahead
(?:-?\d){7} Match 7 digits separated by an optional -
-?\d-\d$ Match an optional - and the \d-\d$ at the end
) Close the lookahead
\d*-\d+-\d*\d-\d Match possible formats where all hyphens are separated by at least a single digit
$ End of string
Regex demo
My two cents:
^(?=.{11}-\d$)(?:\d+-){3}\d
See the online demo
^ - Start string anchor.
(?= - Open positive lookahead:
.{11}-\d$ - Any character other than newline 11 times followed by a hypen, a single digit and the end string anchor.
) - Close positive lookahead.
(?: - Open non-capture group:
\d+- - 1+ digit followed by an hyphen.
){3} - Close non-capture group and match three times.
\d - Match a single digit.
I guess alternatively even ^(?=.{13}$)(?:\d+-){3}\d$ would work.

Regex to validate numbers before and after decimal excluding comma

I am trying to validate decimal number of 13 digit before and 4 digit after dot excluding comma , i.e comma shouldn't be counted as a digit.
Valid Cases
1,234,567,890,123.1234
1234567890123.1234
123456789012.1234
1234567890123.123
12345.123
1.2
0
In Valid Cases
12345abc.23 // string or special characters not allowed
1,234,567,890,1231.1234
1,234,567,890,123.12341
12345678901231.1234
1234567890123.12341
Current Regex
^[0-9]{1,13}(\.[0-9]{0,4})?$
The current Regex is counting comma as a digit.
Any help would be great.
You could use a negative lookahead to assert what is directly on the right is not 14 times a digit before matching a dot:
^(?!(?:[^.\s\d]*\d){14})-?\d+(?:,\d{1,3})*(?:\.\d{1,4})?$
Explanation
^ Start of string
-? Optional hyphen
(?! Negative lookahead, assert what follows is not
(?:[^.\s\d]*\d){14} Match not a digit, whitespace char or dot 14 times
) Close lookahead
\d+ Match 1+ digits
(?:,\d{1,3})* Match comma, 1-3 digits and repeat 0+ times (Or use \d+)
(?:\.\d{1,3})? Optional part, match a dot and 1-4 digits
$ End of the string
Regex demo
You could just specify the optional count of , Like
^[0-9]{0,1}([,])?[0-9]{0,3}([,])?[0-9]{0,3}([,])?[0-9]{1,3}(\.[0-9]{0,3})?$