This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I'm trying to verify card numbers using REGEX. All characters must be digits (0-9), if the first digit is 3 the number can either be 15 or 16 characters long, if the number doesn't start with 3, it must be 16 characters long.
Is there a way to use conditional statements in pure regex, not using an external library?
Similar to #withrp answer but with lookaheads
^(?=[3])([0-9]{15,16})|(?=[0-24-9])([0-9]{16})$
Yes, you can make regex conditional using operator | (OR)
/(^3\d{14,15}$)|(^[^3]\d{16}$)/
Yes, it is possible with a regexp like this where | is used as a logical OR, and intial conditions may be simplified:
String regex = "^(3\\d{14}|\\d{16})$";
where:
3\\d{14} - check the first condition - start with 3, could be 15 chars long (including leading 3)
OR
\\d{16} - any digits, strictly 16 chars long
online demo
Related
This question already has answers here:
How to match letters only using java regex, matches method?
(5 answers)
Closed 3 years ago.
I need a regular expression which should have at least 2 letters.
Total length should be between 6 & 50 characters and can as well include these special characters - _ . #
I came up with this line but it still accepts when I have in the input 1 letter and one special character with a minimum of 6 characters in total.
Where there should be 2 letters minimum for the input to be valid.
^(?=(?:\d*\D){2})[a-zA-Z0-9-_#\\.]{6,50}$
I expect it to be valid when I give for example : a12#45b
And expect to be invalid when I give for example : a1234# (this output gives me valid with expression I have now)
Matching not a digit \D does not guarantee a character a-z.
You could update the positive lookahead to assert 2 times not a-zA-Z and then a-zA-Z. Note that you don't have to escape the dot in the character class.
^(?=(?:[^a-zA-Z]*[A-Za-z]){2})[a-zA-Z0-9-_#.]{6,50}$
Or as suggested in the comment by #Thomas make the pattern case insensitive and use only a-z:
^(?i)(?=(?:[^a-z]*[a-z]){2})[a-z0-9-_#.]{6,50}$
This question already has answers here:
Regular expression to match any character being repeated more than 10 times
(8 answers)
Closed 4 years ago.
I'm trying to find a regex which finds 3 repeating characters appearing contiguously in a string. The character set could be alphabet, digit or any special character.
I wanted the first try for alphabet and digits and then extend the expression to include special characters. The ones I tried are. Both of these fail for the string "c2sssFg1". What am I doing wrong here?
(\\w*)\\2{3,}(\\w*)
(\\w*?)(\\w)\\2{3,}(\\w*)
I looked at some of the examples on SO and on web but I didn't find the right solution that passes the random strings I test.
Can someone help me with this?
Thanks.
(.)\1{2}
(.) matches any char
\1 matches that exactly char
{2} is to grant its 2 more of that
Try (.)\1\1. It works for general case.
This question already has answers here:
Python regex for int with at least 4 digits
(3 answers)
Closed 5 years ago.
Hello all and have a lovely day. I want to ask silly question because i am struggling to find the correct pattern. I want to find all numeric characters which are less or greater than length 6.
Let's take a look below. For instance i have this sequence
12134 4aRt32212121a 11111111111 222222 asda383652re5
My solution is this
\b[0-9]{1,5}\b|\b[0-9]{7,20}\b
What i got as matched is this
12134 11111111111
My problem is that my regex not match any numeric from here 4aRt32212121a. My desired match will be this one
12134 4 32212121 11111111111 5
The numbers will be excluded from match will be 222222,383652 because of the length number equal to 6
i used this Regex online tool to made my tests. you can make the example here if possible.i would appreciate a lot any kind of help and again forgive me for my stupidness
As Eily mentioned in other comment the first issue is \b. This is an anchor for word boundary so it will not match the numbers that are in words like you suggested.
My solution is to remove \b and to make sure you don't get any weirdness add negative lookahead and negative lookbehind and the end and start of your search.
(?<!\d)(\d{1,5}|\d{7,})(?!\d)
edit: accidently typed {1,6} instead of {1,5}
\b means word boundary, ie any place that is not between two \w characters, where \w means numbers, letter or _. In 4aRt32212121a the 3 comes after a letter, so \b\d can't match it. Just remove all your \b.
Edited: And since you don't want to match the 5 digit number inside 111111 you need boundary conditions. With look around assertions you can use:
(?<!\d)(?:[0-9]{1,5}|[0-9]{7,20})(?!\d)
Otherwise (the debugger you have linked to doesn't support them) you have to include either a line boundary or character in the match:
(?:^|[^\d])(?:[0-9]{1,5}|[0-9]{7,20}) (?:$|[^\d])
Hello everyone,
I m new to regex and trying to apply two specific regexes in java.
1 - Regex starts with 79, contain only numbers and length must be 9. My solution is
^(79)\\d{9}$
But not matching this string. 791234567.
2 - Another regex is start with 79 or 78, contain only numbers and length must be 10.
My solution is ^(79-78)\\d{10}$
Both are different and need different regex for each case. Any help would be great.
using a look ahead assertion
^(?=79)\d{9}$
^(?=79|78)\d{10}$
otherwise matching first two character then 7 or 8 remaining
^79\d{7}$
^7[89]\d{8}$
regex101
Here are the regex:
^79\\d{7}$ and ^7(8|9)\\d{8}$
Now for the explanation:
"79" has two characters in it, therefore 79\\d{9} would match 11 characters
(79-78) is not what you thought it would be, it is actually just capturing the characters "79-78" in this specific order, what you want is alternation : (79|78)
78 and 79 have the "7" in common, so (79|78) can become 7(9|8)
"79" still has 2 characters in it, therefore you'd need to match only 8 digits afterward
\\d{9}
means (another) 9 digits.
You need 7 (9 - 2 you already used)
same for the 2nd question,
use this instead
\\d{8}
to indicate 8 additional numbers
Your regex does not work because you did not account for the 2 characters that 79 occupies.
It should be
^79\d{7}$
This means "starting with 79, with 7 more digits following it". All together there are exactly 9 characters.
Your second regex does not work because - does not mean "or". | means "or".
^(79|78)\d{8}$
Again, it should be \d{8} instead of \d{10} for the same reason.
This question already has answers here:
Regular expression to match standard 10 digit phone number
(23 answers)
Closed 6 years ago.
I have this string:
+1(333) 456-7890
I want to match it with a regular expression. This is what I have now for my regex pattern:
Pattern p1 = Pattern.compile("((\\+{0,}[0-9]{0,3}( |-)?)?)(\\(?[0-9]
{3}\\)?|[0-9]{3}( |-)?)([0-9]{3}( |-)?)([0-9]{4}|[a-zA-Z0-9]{7})");
It is supposed to recognize any phone number pattern with potential dashes or spaces in the middle, that could be at least 10 digits (or letters), with no country code and at most 13 digits with a country code.
My pattern seems to match certain cases and not others such as the one previously stated. I'm really stumped, any help would be appreciated.
Your regex seems overly complex, which is why it probably breaks somewhere. I tried to follow it but in the end it was easier to read your definition and build a new one from scratch:
(\+\d{1,3}[- ]*)?(\(?\d\d\d\)?)[- ]*(\d\d\d[- ]?\d\d\d\d|[a-zA-Z0-9]{7})
This matches the following test cases:
+1(333) 456-7890
+1-(212) 555-0198
+1 212 555-0198
+1 212-ILOVEUU
Depending on your own test cases, this might be enough. Or not.
(\+\d{1,3}[- ]*)? // Optional +xxx international prefix plus dash/space
(\(?\d\d\d\)?)[- ]* // three-digit area code with optional parens
(\d\d\d[- ]?\d\d\d\d| // Either 7 digits, with optional dash
[a-zA-Z0-9]{7}) // or 7 letters/digits