Regular expression for 7 digit numbers separeted by commas - regex

I need a regular expression to validate a concatenated string that consists of 7 digit numbers separated by commas.
Furthermore, I must ensure that:
The string is not empty.
The chain doesn't begins or finish with commas.
The numbers do not start with 0.
Example: 1234567,2345678,3456789
My solution so far: ^\d+(,\d+)*?$
The problems I still need to resolve:
Validate that the numbers are exactly 7 digits.
Validate that the numbers do not start with 0.
Thank you.

Something like ^[1-9]\d{6}(,[1-9]\d{6})+$ should work. The [1-9] ensures the number doesn't begin with 0, and \d{6} ensures that there are 6 digits to follow.

Based on Gavin answer, here is what worked for me : ^[1-9]\d{6}(,[1-9]\d{6})*$
The minor difference is the use of the * instead of + at the end of the regular expression. There are some cases where I must validate only one 7 digits number...
Thank you for the help everyone!

Related

Regex for validation of a street number

I'm using an online tool to create contests. In order to send prizes, there's a form in there asking for user information (first name, last name, address,... etc).
There's an option to use regular expressions to validate the data entered in this form.
I'm struggling with the regular expression to put for the street number (I'm located in Belgium).
A street number can be the following:
1234
1234a
1234a12
begins with a number (max 4 digits)
can have letters as well (max 2 char)
Can have numbers after the letter(s) (max3)
I came up with the following expression:
^([0-9]{1,4})([A-Za-z]{1,2})?([0-9]{1,3})?$
But the problem is that as letters and second part of numbers are optional, it allows to enter numbers with up to 8 digits, which is not optimal.
1234 (first group)(no letters in the second group) 5678 (third group)
If one of you can tip me on how to achieve the expected result, it would be greatly appreciated !
You might use this regex:
^\d{1,4}([a-zA-Z]{1,2}\d{1,3}|[a-zA-Z]{1,2}|)$
where:
\d{1,4} - 1-4 digits
([a-zA-Z]{1,2}\d{1,3}|[a-zA-Z]{1,2}|) - optional group, which can be
[a-zA-Z]{1,2}\d{1,3} - 1-2 letters + 1-3 digits
or
[a-zA-Z]{1,2} - 1-2 letters
or
empty
\d{0,4}[a-zA-Z]{0,2}\d{0,3}
\d{0,4} The first groupe matches a number with 4 digits max
[a-zA-Z]{0,2} The second groupe matches a char with 2 digit in max
\d{0,3} The first groupe matches a number with 3 digits max
You have to keep the last two groups together, not allowing the last one to be present, if the second isn't, e.g.
^\d{1,4}(?:[a-zA-z]{1,2}\d{0,3})?$
or a little less optimized (but showing the approach a bit better)
^\d{1,4}(?:[a-zA-z]{1,2}(?:\d{1,3})?)?$
As you are using this for a validation I assumed that you don't need the capturing groups and replaced them with non-capturing ones.
You might want to change the first number check to [1-9]\d{0,3} to disallow leading zeros.
Thank you so much for your answers ! I tried Sebastian's solution :
^\d{1,4}(?:[a-zA-z]{1,2}\d{0,3})?$
And it works like a charm ! I still don't really understand what the ":" stand for, but I'll try to figure it out next time i have to fiddle with Regex !
Have a nice day,
Stan
The first digit cannot be 0.
There shouldn't be other symbols before and after the number.
So:
^[1-9]\d{0,3}(?:[a-zA-Z]{1,2}\d{0,3})?$
The ?: combination means that the () construction does not create a matching substring.
Here is the regex with tests for it.

Regex to categorize phone numbers that 2 of the last 4 digits the same.

Regex to categorize phone numbers. Numbers with 2 of the same digit in the last 4 not adjacent to each other are easier to remember and therefore more valuable. So given 10 digit number how can I match if 2 of the last 4 digits are the same non consecutively? Ex. 2155553747, 2158558284, 7034651215. Thanks in advance for the help.
If you want to use a regular expression for that, and you are okay with the condition, that at least 2 digits of the last 4 digits are the same, you could use the following regular expression:
^\d{6}(?:(\d)\d\d\1|(\d)\d\2\d|(\d)\3\d\d|\d(\d)\d\4|\d(\d)\5\d|\d\d(\d)\6)$
Here is a live example: https://regex101.com/r/t6n1uP/1
Masochistic approach:
/^\d{6}(\d?0[^0]{1,2}0|\d?1[^1]{1,2}1|\d?2[^2]{1,2}2|\d?3[^3]{1,2}3|\d?4[^4]{1,2}4|\d?5[^5]{1,2}5|\d?6[^6]{1,2}6|\d?7[^7]{1,2}7|\d?8[^8]{1,2}8|\d?9[^9]{1,2}9)/m
A test
Far from ideal, but something to start from

Regex, allow characters and digits, but allow up to 7 digits only

I would very much appreciate a bit of help with the following regex riddle.
I need regex statement that would validate against the following rules:
The input can contain letters, special characters and digits.
The input can't start with "0",
The input Can have up to 7 digits
Examples of valid input:
aa1234aa2.(less than 7 digits)
asd234566 (less than 7 digits)
Examples of invalid input:
0asdfd92 (starts with 0)
asd12312311 (more than 7 digits)
What I have tried so far:
^\D[0-9]{0,7}$,
validates against d0000000, but the input may be d0d0dddd1234d
The part can't start with 0 can be removed from the requirement if it complicates a lot. The most important is to have "Can have up to 7 digits" part.
Regards,
Oleg
This is what you need!
Attempt 1: ^[1-9]\d{0,6}$
Attempt 2: ^[^0][\d\w]{0,6}$
Attempt 3: ^[^0].{0,6}$
Attempt 4: ^([\D]*\d){0,7}[\D]*$
Attempt 5: ^([\D]*[1-9]){0,7}[\D]*$|^[^0]\d{0,6}$
Attempt 6: ^([\D]*[1-9]){1,7}[\D]*$|^[^0]\d{1,6}$ <- this should work
Example here
If I understand the requirements correctly, this will work:
^(?=[^0])(\D*\d){0,7}\D*$
That will allow any string that does not start with a zero and has 7 or fewer digits. Any other characters are allowed in any quantity.
Explanation
The first part (?=[^0]) is an assertion that checks to make sure the string does not start with zero. The rest matches any number of non-digits followed by a digit, up to 7 times. Then any number of non-digits before the end of the string.
Assuming Perl (it looks like Perl regular expressions):
Check for leading zero: if (subst($pass, 0, 1) eq '0') { fail }
Check for no more than seven digits: if (($pass =~ tr /0-9/0-9/) > 7) { fail }
I'm generally against trying to cram everything into a single regular expression, especially when there are other tools available to do the job. In this case, the tr will not be executed if there is a leading zero, and a leading zero is easy to spot in the beginning of a string.
Doing it this way, it's easy to add further restrictions independently of the others. For example, "there may be more than 7 digits if they are all separated by other types of characters" (a regex for this one, probably).
You can use this regex:
^[^0](?:\D*\d){1,7}\D*$
RegEx Demo
This will perform following validations:
Must start with non-zero
Has 1 to 7 digits after first char
Verbose, but does the trick.
(^[1-9][^\d]*([\d]?[^\d]*){0,6}$|^[^\d]+([\d]?[^\d]*){0,7}$)
I found it easier to split the RegEx into two cases: when the string starts with a digit, and when it doesn't.
^((?:\D+(?:\d?\D*){0,7})|(?:[1-9]\D*(?:\d?\D*){0,6}))$
You can test it here

REGEX Parameters

I have a notepad with data that looks like this:
"$7.49"
"$124.00"
"$530.00"
How can I search through a range using regular expressions like 200-1000, but that values must be in "$XXX.XX" format.
Thanks for the help!
You can't easily manipulate numbers through regular expressions. It's doable though, so let's look at what you want to have.
Numbers are in the form \d+\.\d+, with no preceding zeroes.
You want to match numbers superior or equal to 200.
You want to match numbers lower or equal to 1000.
So, we have to look at our numbers like they are strings of characters. With the exception of 1000, all of those have three digits. So your regex is something like:
\$([2-9]\d\d\.\d\d|1000\.00)
That is, "a number with three digits left to the dot and the first one is 2 or higher or 1000.00".
This works with $XXX.XX \$\d+\.\d+. The problem with ranges are that numbers used in 3 digits bleed over to 4 digits and the regex gets way more complicated.
Use regex sets [ ] to limit the numbers/values.
So I would search for similar digits such as 200-999.xx which would be
\$[2-9]\d\d\.\d\d
or for four digits for 1000-1500
\$1[1-5]\d\d\.\d\d
If by range you mean number of digits, then go: \$\d{1,3}\,\d{1,2}

Regular expression for Less or more than 9 digits Repeating digits for first 5 or all 9 digits and only number?

I have this
"^(?!(11111|22222|33333|44444|55555|66666|77777|88888|99999|00000))([0-9]\d{8})"
regular expression in c# code and javascript works fine but in text tag of adobe echosign doesnt works anyone have anotherway to work on texttag of echosign?
By your last comment, you need a regex to validate a string of maximum length 9 containing digits only:
^[0-9]{1,9}$
This will validate any string containing digits, with length at least 1 and not greater than 9.
If you want to avoid strings such as 098 (leading zeroes), use this instead:
^[1-9][0-9]{0,8}$
EDIT: If I understand your question well now, you can use this regex:
^(?!([0-9])\1\1-\1\1)[0-9]{3}-[0-9]{2}-[0-9]{4}$
That is assuming that echosign can handle callbacks, if not, you can use this instead:
^(?!(?:111-11|222-22|333-33|444-44|555-55|666-66|777-77|888-88|999-99|000-00))[0-9]{3}-[0-9]{2}-[0-9]{4}$