Check if a String matches specific regular expression

13,881

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$
Share:
13,881

Related videos on Youtube

Shahe Masoyan
Author by

Shahe Masoyan

Updated on June 04, 2022

Comments

  • Shahe Masoyan
    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 the D means the # of days H means hours and M means minutes. I need the regular expression to check the String. Thanks

    • Shahe Masoyan
      Shahe Masoyan over 10 years
      I don't get it. Why the downvote !! It is a constructive question
    • Bohemian
      Bohemian over 10 years
      Probably 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
      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
      Bohemian over 10 years
      So 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
      Shahe Masoyan over 10 years
      @Bohemian no there is not maximum for the days part
    • Bohemian
      Bohemian over 10 years
      OK - see my updated answer that meets your extended requirements
  • Shahe Masoyan
    Shahe Masoyan over 10 years
    thanks @anubhava for the answer, I have a question though, what does the numbers in the curly brackets mean?
  • anubhava
    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
    anubhava over 10 years
    It will also match 111125D8H15MMMM
  • Masudul
    Masudul over 10 years
    @anubhava, nope, I have just tested.
  • anubhava
    anubhava over 10 years
    String#matches adds ^ and $ but if same regex is used in Pattern then it will match bigger string.
  • Bohemian
    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
    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
    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 in Pattern/Matcher with Matcher#find then it is different thing.