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.
Related
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
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.
I need regular expression which will start with 2 specific letters and will be 28 characters long.
The regular expression is needed, as this is in conjunction with Spring configuration, which will only take a regular expression.
I've been trying to do with this, it's not working (^[AK][28]*)
If you mean that the string should be like "AKxxxxxxxx" (28 characters in total), then you can use:
^AK.{26}$ //using 26 since AK already count for 2 characters
Regex is nothing specific to Java, nor is it that difficult if you have a look at any tutorial (and there's plenty!).
To answer your question:
AK[a-zA-Z]{26}
The above regex should solve your issue regarding a 28 character String with the first two letters being AK.
Elaboration:
AK[a-zA-Z]{26}> Characters written as such, without any special characters will be matched as is (that means they must be where they were written, in exactly that fashion)
AK[a-zA-Z]{26}> By using square brackets you can define a set of characters, signs, etc. to be matched against a part of the regex (1 by default) - you can write down all the possible characters/signs or make use of groups (e.g. a-z, /d for digits, and so forth)
AK[a-zA-Z]{26}> for each set of characters/signs you can define a repetition count, this defines how often the set can/must be applied. E.g. {26} means it must match 26 times. Other possibilities are {2, 26} meaning it must match at least 2 times but at most 26 times, or for example use an operator like *, + or ? which denote that the set can be matched 0 or more times, at least once or 0 or 1 time
In case you need it matching a whole line you would likely want to add ^ and $ at the beginning and end respectively, to tell the regex parser that it has to match a whole line/String and not just a part:
^AK[a-zA-Z]{26}$
If you need to count the number of repetitions use the {min, max} syntax. Omiting both the comma and max tells the regex parser to look for exactly minrepetitions.
For example :
.{1,3} will match for any character (shown by the dot) sequence between 1 and 3 characters long.
[AK]{2} will match for exactly 2 characters that are either A or K :
AK, AA, KA or KK.
Additionnaly, your regex uses [AK]. This means that it will match against one of the characters given, i.e. A or K.
If you need to match for the specific "AK" sequence then you need to get rid of the '[' ']' tokens.
Therefore you regex could be AK.{28} meaning it will match for AK followed by exactly 28 characters.
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.
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