Check if a String matches specific regular expression
Solution 1
Here's the briefest way to code the regex:
if (str.matches("(?!$)(\\d+D)?(\\d\\d?H)?(\\d\\d?M)?"))
// format is correct
This allows each part to be optional, but the negative look ahead for end-of-input at the start means there must be something there.
Note how with java you don't have to code the start (^
) and end ($
) of input, because String.matches()
must match the whole string, so start and end are implied.
However, this is just a rudimentary regex, because 99D99H99M
will pass. The regex for a valid format would be:
if (str.matches("(?!$)(\\d+D)?([0-5]?\\dH)?([0-5]?\\dM)?"))
// format is correct
This restricts the hours and minutes to 0-59
, allowing an optional leading zero for values in the range 0-9
.
Solution 2
Try,
String regex = "\\d{1,2}D\\d{1,2}H\\d{1,2}M";
String str = "25D8H15M";
System.out.println(str.matches(regex));
Solution 3
A simplified regex can be:
^\\d{1,2}D\\d{1,2}H\\d{1,2}M$
Related videos on Youtube
Shahe Masoyan
Updated on June 04, 2022Comments
-
Shahe Masoyan almost 2 years
I am not so good with regular expressions and stuff, so I need help. I have to check if a input value matches a specific regular expression format. Here is the format I want to use,
25D8H15M
. Here theD
means the # of daysH
means hours andM
means minutes. I need the regular expression to check the String. Thanks-
Shahe Masoyan over 10 yearsI don't get it. Why the downvote !! It is a constructive question
-
Bohemian over 10 yearsProbably downvoted (not by me) because you have shown no effort to attempt a solution or even to describe the key points of one.
-
Shahe Masoyan over 10 years@Bohemian, what I need to do is: check if the given string is valid, valid values are: 1D - 2D15H - 5H33M - 22D30M - 15D12H45M etc.
-
Bohemian over 10 yearsSo all parts are optional, but there must be at least one part? Is
3D5M
valid? Is there a maximum number value for the days part? -
Shahe Masoyan over 10 years@Bohemian no there is not maximum for the days part
-
Bohemian over 10 yearsOK - see my updated answer that meets your extended requirements
-
-
Shahe Masoyan over 10 yearsthanks @anubhava for the answer, I have a question though, what does the numbers in the curly brackets mean?
-
anubhava over 10 years
\\d{1,2}
means match 1 or 2 digits these numbers mean lower limit and upper limit of # of matched patterns. -
anubhava over 10 yearsIt will also match
111125D8H15MMMM
-
Masudul over 10 years@anubhava, nope, I have just tested.
-
anubhava over 10 years
String#matches
adds^ and $
but if same regex is used in Pattern then it will match bigger string. -
Bohemian over 10 years@anubhava with java you don't have to code the start (^) and end ($) of input, because
String.matches()
must match the whole string, so start and end are implied -
Bohemian over 10 years@Masud the character classes are unnecessary here, ie
\\d{1,2}
is identical to[\\d]{1,2}
because there's only one character (type) -
anubhava over 10 years@Bohemian: That is what I had written in my comment that
String#matches adds ^ and $
but if this regex is used inPattern/Matcher
withMatcher#find
then it is different thing.