• Post Reply Bookmark Topic Watch Topic
  • New Topic

Could anyone please help me in understanding this pattern  RSS feed

 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's have a look at the regular expression and break it up into parts:

  • [^0-9]* This matches zero or more characters that are not digits.
  • [12]? This matches one character that must be 1 or 2.
  • [0-9]{1,2} This matches one or two characters that must be digits.
  • [^0-9]* This matches zero or more characters that are not digits (same as the first).


  • [0-9] means: characters in the range 0 to 9 (so, 0, 1, 2, ..., 9)
    [^0-9] means: all characters except those in the range 0 to 9
    * means: zero or more characters
    ? means: exactly one character
    {1,2} means: between one and two characters (so, exactly one or two characters)

    So, this matches zero or more non-digits, followed by a "1" or "2", then two digits, then zero or more non-digits.

    Note that you can find a description of regular expression syntax in the API documentation of class java.util.regex.Pattern.

    p.s. "Hundred" is written with a "d" at the end, "Hundret" is wrong; also, it should have been "Than" instead of "Then"; "Then" means you're talking about time (in the past) -> isLessThanThreeHundred
     
    Jacob Sonia
    Ranch Hand
    Posts: 183
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the great explanation.
     
    Paul Clapham
    Sheriff
    Posts: 22819
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Now... look at it critically and see if the method deserves that name (or, alternatively, if the comment at the top is correct).

    I see at least one reason why not. Can you find it?
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!