Can I make this Phone Validator Regex more efficient? - java

I started a few days ago to mess with Regex and today I was requested to make a fast regex. (Is it really an art to create efficient regexes?)
So I wrote this simple Regex to match an Israeli Phone Number:
^05[23489]-?[\d]{3}-?[\d]{4}$
But will it do the job which is to complete verifying around 10,000 phone numbers within 1 or 2 seconds? I don't have a computer here so I can't check.
Thanks for any improvement!
The match rules are:
Starts with 05, then one of: 0, 2, 3, 4, 8, 9
then Optional Hyphen
then 3 digits
then Optional '-'
Ultimately come 4 digits.
A few examples to Valid phone numbers:
052-587-6549
0531432941
058-3219321
059-321-1353

The [23489] does not include 0. Besides, it is not a good idea to wrap single \d with character class brackets, [\d] = \d.
Use
^05[023489]-?\d{3}-?\d{4}$
^
See the regex demo.
In Java, you do not need ^ and $ anchors if you use the pattern with .matches() method as itrequires a full string match.
if (phone.matches("05[023489]-?\\d{3}-?\\d{4}")) {
// This is valid
}

Related

Need regex for a string having characters followed by even number of digits

Can anyone tell how I can write regex for a string that take one or more alphanumeric character followed by an even number of digits?
Valid:
a11a1121
bbbb11a1121
Invalid:
a11a1
I have tried ^[a-zA-Z*20-9]*$ but it is always giving true.
Can you please help in this regard?
The regex that you have mentioned will search for any number of [either a-z, or A-Z or 2 or 0-9]
You can break down your requirement to groups and then handle it accordingly.
Like you require at least one character. so you start with ^([a-zA-Z]+)$
Then you need numbers in the multiple of 2. so you add ^([a-zA-Z]+(\d\d)+)$
Now you need any number of combination of these. So the exp becomes: ^([a-zA-Z]+(\d\d)+)*$
You can use online tools like regex101 for these purpose. The provided regex in action here
You can achieve it with this regexp: ^[a-z0-9]*[a-z]+([0-9]{2})*$
Explanation :
[a-z0-9]*[a-z]+: a string of at least one character terminated by a non digit one
([0-9]{2})*: an odd sequence of digits (0 or 2*n digits). If the even sequence cannot be null, use ([0-9]{2})+ instead.

What is a regular expression that will match a character at certain indexes?

Is there a single regular expression that will match strings that have the letter "a" or "x" at certain indexes?
For example, the indexes may be
0, 3, 6
1, 4, 7
2, 5, 8
Possible patterns according to these indexes:
"a--a--a--"
"-x--x--x-"
"--a--a--a"
Other occurrences of "a" or "x" are okay. I just want to make sure certain indexes contain "a".
EDIT: This problem is a subproblem of a tic-tac-toe board. I'm to determine if there is a winner by using a regular expression. I don't want the answer to this. I just want a bit of a push to help me move toward the final answer. The above is to help me find a regex that would identify if there is a vertical winner.
If your tic-tac-toe looks like a 9 chars long string, testing this pattern should suffice to know if there's a vertical line:
a..a..a
You can use this regex:
([a-zA-Z])..\1..\1
RegEx Demo
([a-zA-Z]) will match and capture any letter in group #`
Then another 2 instances of same letter at n+3 and n+6 positions are matched using back-reference \1, where n is the first position of letter.
Since a appears every 3 characters, (a..)* will match those repetitions. Then you just need to precede that with a pattern that matches the required number of characters before it, which is simply n-1 . patterns.
So for the three examples you gave, it's
^(a..)*
^.(a..)*
^..(a..)*

Regex expression for timestamp with or without leading zero?

I'm struggling with Regex.
This is a sample timestamp: 00:00:00.00 (Hour, Minutes, Second.Decimal). I also want this value to match 00:0:0.00 Notice that the leasing zero is optional in the center.
I was using this: [1-60]:[1-60]:[1-60].[1-100], but that requires no leading zero. I would like help with making a SINGLE regex that works for both of the things listed above.
A complete solution would be fantastic, but if you could just point me in the right direction, that would be helpful as well.
Your solution won't actually match what you've described; it will only match a single digit in the sequence 0123456 in each position. You probably want something like
[0-5]?\d:[0-5]?\d:[0-5]?\d\.\d{1,2}
Your pattern has a number of other problems. [1-60] is a character class. It will match a single 1, 2, 3, 4, 5, 6, or 0 character. Secondly, the . in your pattern matches any character not just a literal ..
I think what you're looking for is something like this instead:
\d{1,2}:\d{1,2}:\d{1,2}\.\d{1,2}
This will match any one or two digits, followed by a literal :, followed by any one or two digits, followed by a literal :, followed by any one or two digits, followed by a literal ., followd by any one or two digits.
Or to check match only particular ranges of each time component, you can use a pattern like what chrylis suggests, although I'd generally recommend actually parsing the time value if you really need to do this.
Another option you could do:
(?:\d{1,2}:){2}\d{1,2}\.\d{1,2}
Regular expression:
(?: group, but do not capture (2 times):
\d{1,2} digits (0-9) (between 1 and 2 times)
: ':'
){2} end of grouping
\d{1,2} digits (0-9) (between 1 and 2 times)
\. '.'
\d{1,2} digits (0-9) (between 1 and 2 times)

Reg Expression Validation on a String

Can I use Reg Expression for the following use case?
I Need to write a boolean method which takes a String parameter that should satisfy following conditions.
20 character length string.
First 9 characters will be a number
Next 2 characters will be alphabets
Next 2 characters will be a number.(1 to 31 or 99)
Next 1 character will be an alphabet
Last 6 characters will be a number.
In this, I have wrote the code for the first requirement:
[a-zA-Z0-9]{20} - This expression works well for the first case. I don't know how to write a complete reg expression to meet the entire requirement.
Please help.
Yes, it is possible to use regexes for this.
Ignore the "20 characters" part and describe a string created by concatenating 9 digits, 2 letters, 2 digits, 1 letter and another digit.
Start with the string start: ^
Then 9 digits. The \d conveniently describes the character set [0-9], so \d{9} means "nine digits"
Then 2 letters. The \w class is too broad, so stick to [a-zA-Z] for a letter.
Then another two digits. They seem to be from a restricted set, so describe the set with alternation and grouping.
Then another letter and another digit.
And, finally, you have to end at the end of the string: $
For reference, this regex means "the string is nine letters, then 12-15 or 99, then another letter":
^[a-zA-Z]{9}(1[2-5]|99)[a-zA-Z]$
Read the String JavaDocs, especially the part about String.matches() as well as the documentation about regular expressions in Java.
Your first requirement is already implicit in the remaining ones, so I would just skip it. Then, just write the regex code that matches each part one after the other:
[0-9]{9}[a-zA-Z]{2}...
There is one special consideration for the number that might be 1 to 31. While it is possible to match this in one regex, it would be verbose and difficult to understand. Instead, perform basic matching in the regex and extract this part as a capturing group by putting it into parentheses:
([0-9]{2})
If you use Pattern and Matcher to apply your regex, and your string matches the pattern, you can then easily get at just thost two characters, use Integer.parseInt() to convert them to an integer (which is completely safe because you know the two characters are digits), and then check the value normally.
This regular expression takes
^[0-9]{9}[a-zA-Z]{2}([1-9]|[1-2][0-9]|3[0-1]|99)[a-zA-Z]([0-9]{6})$
takes
9 letters at start,
Followed by 2 alphabets,
Followed by number between 1 to 31 or 99,
Followed by an alphabet,
followed by 6 digits.

regex to parse a string in java

I am writing a simple regex in java, and for some reasons my regx is not working.
What I want to achieve is to parse a string that is,
Starts with number 9
Has exactly 10 digit (including prefix 9)
My Regex is (^9\\d[0-9]{10}) and I want to parse as an example, 91234567890. But it is not working.
You shouldn't have escaped the [ (because that made your regex expect a literal [ after the 9).
Also, 1 + 10 = 11, so you need to lower the quantifier.
Finally, use the end-of-string anchor $ to make sure that no other characters occur after the 10th digit:
^9[0-9]{9}$
9[0-9]{9}
should work. It looks for the number 9, followed by 9 more numbers

Categories

Resources