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
Related
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
}
I'm trying to write regex of exact 6 characters long, where first three characters are static (ABC) and last 3 characters are numbers (0-9).
Here is my regex:
^[ABC][0-9]{3}$
But I'm not getting results. Any suggestion?
Use:
^ABC[0-9]{3}$
[ABC] - means one character from this set.
Try to play with this regex using online tool. It will be much easier to understand and develop it.
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.
I am working with a calculator , after writing up the whole statement in the EditText, I want to extract the numbers and calculator symbols from the edit text field. The numbers can be simple integers or decimal numbers, i.e; Float and Double. For example I have this string now, 2 + 2.7 - 10 x 20.000. The regex will extract the +,- and x separately and the numbers as; 2,2.7,10,20.000. Basically i need to regex for each.
The regex you're searching for will be different from the regex from the split. If you use a Pattern for this, things will get really slow and complicated. So here we're using String#split().
Here is your regex:
"\s*([^\d.]+)\s*"
Regex Explanation:
\s* - Matches every white space if available
([^\d.]+) - Capture everything but a digit and a dot together as many as possible
\s* - Matches every whitespace if available
Note[1]: Once again, as it's going to be used in the split, you don't want to capture the numbers, this regex will match anything but numbers and remove them from the string, so if you use them in a Pattern#exec() you'll have a different output.
So this way we can simply:
"2 + 2.7 - 10 x 20.000".split("\\s*([^\\d.]+)\\s*");
Note[2]: The regex inside a String in Java must have its backslashes escaped like above's. And also, the capture groups (...) are useless since we're just splitting it, feel free to remove them if you want.
JShell Output:
$1 ==> String[4] { "2", "2.7", "10", "20.000" }
Java snippet:
https://ideone.com/CHjKNB
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.