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.
Related
I generate a 6 characters long random number. It can be all numeric, alphabets and alphanumeric. I have to validate this string on basis of provided regular expression. For example:
If string is numeric [0-9], then it should not contain all zeroes.
If string is alphabetic [a-zA-Z], then last character cannot be X or x. And string cannot start with SVC or svc.
If string is alphanumeric [0-9a-zA-Z], then it cannot contain all zeroes 0. And string cannot start with tripple zeroes 000 and cannot end with x or X.
I need regular expressions for these that can be used with Java Matcher.
This should work:
/^((?!.{7,})[0-9]*[1-9]+[0-9]*|(?!(SVC|svc))[a-zA-Z]{5}[a-wy-zA-WY-Z]|(?!(000|.{7,}))[0-9a-zA-Z]*([a-zA-Z][0-9]|[0-9][a-zA-Z])+[0-9a-zA-Z]*[a-wy-zA-WY-Z])$/gm
I cannot explain this regex, as it is too long and just a repetitive application of the same concepts over and over. However, given the following input, it matches only the first five lines:
002000
jfkasd
002dfd
sVcabc
abc65i
000000
00012c
0123ax
SVCabx
svcabc
abc65x
abc65X
Here's the original attempt I proposed, which does not satisfy all the condition of the OP, but it is accompained by an explanation:
/^((?!.{7,})[0-9]*[1-9]+[0-9]*|[a-zA-Z]{5}[a-wy-zA-WY-Z]|(?!000)[0-9a-zA-Z]{6})$/gm
Explanation (which could read on the linked page itself):
We have three alternatives that have to match the whole line: ^(…|…|…)$;
The 2nd alernative is easy: five letters followed by one letter which is not x or X, [a-zA-Z]{5}[a-wy-zA-WY-Z] ([^xX] would match numers too or anything else).
The 3rd alternative is slightly more complex: six letters or digits, which is not preceded by 000; this uses a negative lookahead, and it works because of the anchor ^ (if you remove that, it breaks).
The 1st alternative is similar: zero or more digits, followed by one or more non-0 digits, followed by zero or more digits; all not starting by 7 or more characters.
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.
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.
if(password.matches("(?=.*[0-9].*[0-9])(\\w{8,})") )
System.out.println("Valid Password");
else
System.out.println("Invalid Password");
I am checking a password to ensure it has at least 8 characters in length, which can be letters or digits and it must have at least 2 digits. This appears to work for me but I just wanted to confirm I was doing this right. Also, I have been trying to research and figure out exactly what each piece is doing. Below is what I believe each piece to be doing, but if I am incorrect, would you please explain what the particular portion is actually doing. Thanks
?= tells the program to remember if the digits [0-9] which I am looking for are found ?
.* says for any number of [0-9]?
[0-9] Specifies any number from 0-9.
.*[0-9] Then the regex looks for another number from 0-9 ?
(\\w{8,}) looks for any letters (uppercase or lowercase) and digits, with a minimum length of 8 characters?
That regex has two main parts:
(?=.*[0-9].*[0-9])
(\\w{8,})
Part 1. is a positive look ahead, which has the form (?=pattern). "Look arounds" (positive/negative look behinds/aheads) assert, without consuming (or capturing), that the adjacent input matches a certain pattern. In this case, it's asserting that the input following the current point contains (at least) 2 digits (.* meaning 0-n chars, [0-9] meaning any number character). Incidentally, it could be expressed more succinctly as (?=(.*[0-9]){2}
Part 2. means "at least 8 word characters" - a word character is any letter, any number or an underscore. The brackets around it (unnecessarily) capture the 8+ word characters as group 1
?= is a positive look ahead, That means that it is searching for something ahead of it.
http://www.regular-expressions.info/lookaround.html
For more info on the look ahead.
http://rubular.com/
Great for testing out any regex.
I need to check that a file contains some amounts that match a specific format:
between 1 and 15 characters (numbers or ",")
may contains at most one "," separator for decimals
must at least have one number before the separator
this amount is supposed to be in the middle of a string, bounded by alphabetical characters (but we have to exclude the malformed files).
I currently have this:
\d{1,15}(,\d{1,14})?
But it does not match with the requirement as I might catch up to 30 characters here.
Unfortunately, for some reasons that are too long to explain here, I cannot simply pick a substring or use any other java call. The match has to be in a single, java-compatible, regular expression.
^(?=.{1,15}$)\d+(,\d+)?$
^ start of the string
(?=.{1,15}$) positive lookahead to make sure that the total length of string is between 1 and 15
\d+ one or more digit(s)
(,\d+)? optionally followed by a comma and more digits
$ end of the string (not really required as we already checked for it in the lookahead).
You might have to escape backslashes for Java: ^(?=.{1,15}$)\\d+(,\\d+)?$
update: If you're looking for this in the middle of another string, use word boundaries \b instead of string boundaries (^ and $).
\b(?=[\d,]{1,15}\b)\d+(,\d+)?\b
For java:
"\\b(?=[\\d,]{1,15}\\b)\\d+(,\\d+)?\\b"
More readable version:
"\\b(?=[0-9,]{1,15}\\b)[0-9]+(,[0-9]+)?\\b"