Regex with constraints, look-ahead function - regex

I'm trying to write a regex expression between 10 and 12 digits. There will be optional leading 0 (zeros) between {0,5} then a numeric string between 10-12 digits. Regardless of the number of zeros (0 to 5), I want 10-12 digits after leading zeros
Example:
0000012345 should not be passing
0012345678 should not be passing as there are only 8 digits after leading zeros
I've tried:
^(0{0,5}(?=\d{10,12}$)^\d{1,2}?\s?(\d{10})$

I think
^0{0,5}[1-9]\d{9,11}$
should be what you need. It enforces not counting the leading zeroes as one of the later digits by requiring it be non-zero. Then there can be 9-11 other digits (including 0).
If you need to include an optional space at any point (as suggested by your RegEx), the RegEx would grow a lot, and it might be easier to do this with some additional code. However, if you give the exact requirements, I will edit the answer accordingly.

^0{0,5}+\d{0,2}\s?\d{10}$
^^
You didn't specify the language. You need "possessive quantifier" here.
See demo:
https://regex101.com/r/m5sOAJ/1
Or if your regex does not support possessive quantifiers:
^(?=(0{0,5}))\1\d{0,2}\s?\d{10}$
See demo:
https://regex101.com/r/m5sOAJ/3

Related

Regex to detect repetition

I need a regex to detect different forms of repetitions (where the entire word is a multiple of same character/substring). The total length of the word should be minimum 7 (of the whole word, not of the repetitive sequence)
Example - Terms as follows are not allowed
abcdefabcdef
brian
2222222
john12john12
Terms as follows are allowed
hellojohn
2122222222
abcdefabc
The validity of this answer depends on the regular expression engine you are using, as it uses negative look-aheads to effectively "invert" the repeated substring matching. You can play with the regex solution here: https://regex101.com/r/DjmuaI/1/
Short answer: ^(?!(.+?)\1+).{7,}$
Long answer:
Start off by trying to match at least one repetition of a character sequence. This tries to capture a sequence of characters (.+) and uses a back-reference of this captured group \1.
^(.+)\1$
Allow more than 1 repetition by adding + to our capture group back-reference. This now detects a character sequence that is a substring repeated.
^(.+)\1+$
Look for character sequences that are NOT repeating. A negative-lookahead (?!regex) (which support varies between regex engines) allows us to invert the condition.
^(?!(.+?)\1+).+$
However, this would match any non-repetitive string (including strings less than 7 in length). The pattern can be changed to be 7 or more characters using {7,}.
^(?!(.+?)\1+).{7,}$
I will note that matching some strings may be not have great performance.

Regex for All Digits but Not Exclusively Zeros

I'm trying to find a regex that matches inputs that:
are non-empty AND
are not exclusively zeros (although leading zeros are fine) AND
have no non-digits
Put another way, the string must consist entirely of digits, but not exclusively zeros.
You could use a couple lookaheads:
^(?!0*$)(?=\d+$)
https://regex101.com/r/fTgsWE/3/
Or speed version (avoid look around altogether )
^\d*[1-9]\d*$

Regex to match specific number format

I am having trouble figuring out how to write a regex to match a number (technically a string) with the following rules:
all numeric
must be exactly 11 digits
it must start with at least 2 zeros
it may not start with more than 4 zeros
I can use \d{11} to match for the exactly 11 digits, and ^0{2,4] to match the leading zeros part, but I can't figure out how to combine them.
^00(?!000)\d{9}$
It checks for two zeroes and then checks that there are not more than 2 0's following it.
if it is not it checks the other 9 numbers to the end of the string.
This assumes your engine supports lookaheads.

how to find integer with comma and zeros after that (regex)?

I try to create regex(es) to extract all integers. It can be 6 -12 bur also +6.000 or -5,0 and onother one to extract real numbers which are not integers, for example 3.14, -6,26 but no 5.0.
For finding integers I tried "^[+-]?([0-9]+)(\\[.,]0{1,})?$" but it doesn't work on -6.00. And I have no idea how to create second regex (how to exclude integers with comas or dots and then zeros). Any help appreciated.
The problem with your integer regex appears to be the backslash(es). I don't know any regex engine in which you would need to escape the opening bracket of a character class, and you certainly don't want to match a literal backslash. Also, to a regex engine that understands it at all, the quantifier {1,} is an uglier, more complex way of saying +.
This should do your integer matching:
"^[+-]?[0-9]+([.,]0+)?$"
And this variation should do your non-integer matching:
"^[+-]?[0-9]+[.,]0*[1-9][0-9]*$"
In both cases I omitted parentheses not needed for expressing a correct pattern, but if you need to capture parts of the match then you will want to add some back in. You might also want to convert the grouping parentheses into non-capturing form if you are using a regex engine that supports it.
Also, the real number pattern requires at least one digit before the fraction separator character, per your examples. It would be easy to convert the pattern to also match strings of the form .1 or -.17. Similarly, the integer pattern requires at least one zero in the fraction part if there is a fraction separator, and restriction could be removed, too.

Regular expression for optional commas with a 6 character limit

I need a regex that allows numbers and optional commas, but the entire length cannot be greater than 6.
^[0-9]+([,]*[0-9]+)*$ allows numbers and optional commas.
^([0-9]+([,]*[0-9]+)*){0,6}$ does not limit the total length to 6.
If your regex engine supports lookahead assertions — most do — then you can write:
^(?=[0-9,]{1,6}$)[0-9]+(,[0-9]+)*$
The (?=[0-9,]{1,6}$) part is a "positive lookahead assertion", and means "looking forward from this point in the string, I see [0-9,]{1,6}$". So, in essence, the above regex is a combination of these two:
^[0-9,]{1,6}$
^[0-9]+(,[0-9]+)*$
and enforces them both.
(That said, it's likely to be clearer if you simply enforce the length restriction as a separate step, rather than incorporating the above into a single regex.)
^([\,0-9]{0,6})$
This regex simply allows any of the characters (comma, zero through nine) zero through six times.
If you require that the input start with a number, use this:
^([0-9]{1}[\,0-9]{0,5})$
Some additional ways -
^(?=.{1,6}$)\d+(?:,?\d)*$
^(?=.{1,6}$)\d(?:[,\d]*\d)?$