Regular expression for optional commas with a 6 character limit - regex

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)?$

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 with constraints, look-ahead function

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

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.

getting at least 1 of 2 zero or more sets with a regular expression

How would I write a regular expression that allows for zero or more of one group, and zero or more of another group, but at least one of the two groups has to exist?
Specifically, I want to get a spreadsheet like reference, so it should get A1:B5 (for a whole region), A:A (for a whole column), or 5:5 (for a whole row).
I first tried
[A-Za-z]*[\d]*:[A-Za-z]*[\d]*
but this wouldn't be sufficient because then simply typing : or B6: would also satisfy that criteria.
Any help would be appreciated.
You can do this with grouping...
/((how)|(now))+/
If you want to match a range but not a cell reference, you could just enumerate the ways to do that:
([A-Z]:[A-Z])|(\d+:\d+)|([A-Z]\d+:[A-Z]\d+)
One way would be an explicit alternation:
(?:[a-zA-Z]+|\d+|[a-zA-Z]+\d+):(?:[a-zA-Z]+|\d+|[a-zA-Z]+\d+)
If your engine supports lookbehind, however, you could use that:
(?>[a-zA-Z]*\d*(?<=.)):(?>[a-zA-Z]*\d*)(?<=.))
This says "zero or more letters, followed by zero or more numbers, which must end in at least one character (.). That guarantees it won't be empty. The atomic grouping (?>...) means that the lookbehind (?<=.) can't match whatever came before that point.

Regex for a string up to 20 chars long with a comma

I need to define a regex for a string with the following requirements:
Maximum 20 characters
Must be in the form Name,Surname
No numbers and special characters allowed (again, it's a name&surname)
I already tried something like ^[^1-9\?\*\.\?\$\^\_]{1,20}[,][^1-9\?\*\.\?\$\^\_\-]{1,20}$ but as you can find, it also matches a 40 chars long string.
How can I check for the whole string's maximum length and at the same time impose 1 comma inside of it and obviously not at the borders?
Thank you
Try the regex:
^(?=[^,]+,[^,]+$)[a-zA-Z,]{1,20}$
Rubular Link
Explanation:
^ : Start anchor
(?=[^,]+,[^,]+$) : Positive lookahead to ensure string has exactly one comma
surrounded by at least one non-comma character on both sides.
[a-zA-Z,]{1,20} : Ensure entire string is of length max 20 and has only
letters and comma
$ : End anchor
You can do this using forward negative assertions:
^(?!.{21})[A-Za-z]+,[A-Za-z]+$
The regex contains two parts now, the actual definition, and a statement at the start, saying that from that point, there will not be 21 characters.
So for the definition as stated above, the regex becomes
^(?!.{21})[^1-9\?*\.\?\$\^_\,]+,[^1-9\?*\.\?\$\^_\,]+$
The obvious answer would be: Don't ask for name and surname in the same input field.
If you still want to do it: There's no easy way that I know of, but here is a possibility. To see the principle think your [^1-9\?\*\.\?\$\^\_\,] instead of X (I added he \, since it's kind of important :-)).
^(X{1},X{19})|(X{2},X{18})|...|(X{19},X{1})$
Quite ugly, but should work.
On a different note: You don't capture nearly all special characters with your exclusive range. But it's probably still better than an inclusive range.
As I say, I think stated the way you have it, it's not matchable by a regular expression -- it's a pushdown language.
However, you could always split on ',' and match each substring, then total.
I have you tried your example, but removing the
{1,20}
in the middle, leaving to try this:
^[[^1-9\?\*\.\?\$\^\_],[^1-9\?\*\.\?\$\^\_\-]]{1,20}$
Use:
[[a-zA-Z],[a-zA-Z]]{1,20}