regex with optional data at the front - regex

I have a regex that looks like this
((1010xxx)?(\d{11}|\d{10}|\d{7})+)
Basically I want it to match
8085551234
5551234
10102338085551234
and it should fail on
1010233
This is more for validation being done on an xsd than an actual matcher.
PS.
I am trying to match US telephone numbers 7 - 11 digits long with an optional 1010xxx at the front. Also if it is 1010xxx it should not pass. xxx is any 3 digits.

If all you want is to make sure that it's a 7, 10, or 11 digit string, making sure that if it's only 7 digits it doesn't start with '1010', you can use a negative lookahead assertion before your match on \d{7}, i.e.:
((\d{11}|\d{10}|(?!1010)\d{7})+)

Related

Regex for "everything except 34,37" WITHOUT negative lookahead

Every time this is asked, the answer is to use negative lookahead like so: (?!x)
Well unfortunately I must implement an American Express check in vb6, which does NOT support negative lookaheads or negative look behinds.
Is it possible to match everything except 34, 37 using the oldest imaginable implementation of regular expressions?
I expected this sort of thing to work:
^[1,2,4-9][1-3,5-6,8-9][0-9]{13}$
I've been able to match AmEx with this expression of course: ^3[47][0-9]{13}$ but I haven't found a way to inverse the validation rule.
Here is my validator setup:
<asp:RegularExpressionValidator
ID="AmExValidator"
Display="None"
ControlToValidate="txtCreditCardNumber"
ValidationExpression="^[1,2,4-9][1-3,5-6,8-9][0-9]{13}$"
ErrorMessage="American Express cards are not accepted."
ValidationGroup="Payment"
runat="server">
</asp:RegularExpressionValidator>```
You can use
^([0-24-9][0-9]|3[0-35689])[0-9]{13}$
See the regex demo. Details:
^ - start of string
([0-24-9][0-9]|3[0-35689]) - 00 to 29 and 40 to 99, or 3 followed with a digit other than 4 and 7
[0-9]{13} - thirteen digits
$ - end of string.

Trying to create a regex that allowes following format yyyy[: -][VW]Week number

My regex currently looks like this
\b(19|20)\d{2}\b[- :][VW][0-5]{1}(?(?=[5])[0-2]{1}|[0-9]{1})
It doesn't quite do what I want as I'm trying to get this part
(?(?=[5])[0-2]{1}|[0-9]{1})
to say "If the previous number was 5 then you may only choose between 0-2, and if it's another number 0-4 then choosing between 0-9 is allowed
Currently it allowes 00-59 with an exclusion of 05,15,25,35 etc.
Essentially I want it to look like this for example 2016-W25.
You need to replace [5] with a positive lookbehind (?<=5) in order to check a char to the left of the current location:
\b(19|20)\d{2}[- :][VW][0-5](?(?=(?<=5))[0-2]|[0-9])
^^^^^
See the regex demo
Also, you may get rid of the conditional pattern at all using a mere alternation group:
\b(19|20)\d{2}[- :][VW](?:[0-4][0-9]|5[0-2])
^^^^^^^^^^^^^^^^^^^^^
See this regex demo
The (?:[0-4][0-9]|5[0-2]) matches either a digit from 0 to 4 and then any digit (see [0-4][0-9]), or (see |) a 5 followed with 0, 1 or 2 (see 5[0-2]).
NOTE: Since the number of weeks can amount to 53, the [0-2] at the end might be replaced with [0-3] to also match 53 values.

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

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}$

How to match a one of a set of numbers?

I am trying to match a group of numbers in regex that consist of one of the following:
1,2,3,4,5,6,7,8,9,10,11
But I am having trouble figuring out the regex.
For single digits this pattern worked fine "0|1|2|3|4|5|6|7|8|9" but it fails on double digit numbers. For example 12 passes as ok due to the regex finding the 1 in 12.
You can use begin and end anchors to force the whole string to be matched:
^(0|1|2|3|4|5|6|7|8|9|10|11)$
Which can be shortened to:
^(\d|10|11)$
This will work if you want to check if just one number is between 0 and 11.
^[0-9]$|^1?[0-1]$
If you want to match a string like:
1,2,3,12,32,5,1,6,8, 11
and match 0-11 then you can use the following:
(?<=,|^)([0-9]|1?[0-1])(?=,|$)
use this regex ^(0|1|2|3|4|5|6|7|8|9|(10)|(11))$