Learning from Challenge - HackerRank Regex-Application

@date : 170710 ~
@refernence :
Java7 API
Java tutorial Essential
@lauguage : Java

Saying Hi

  • String.matches() vs Matcher.matches()
    • cf.String.matches() vs Matcher.matches()
    • String - matches()
      “An invocation of this method of the form str.matches(regex) yields exactly the same result as the expression Pattern.matches(regex, str).” [JavaAPI]
    • java.util.regex.Pattern:
      “ A compiled representation of a regular expression. A regular expression, specified as a string, must first be compiled into an instance of this class. The resulting pattern can then be used to create a Matcher object that can match arbitrary character sequences against the regular expression. All of the state involved in performing a match resides in the matcher, so many matchers can share the same pattern.

      A typical invocation sequence is thus

      Pattern p = Pattern.compile(“a*b”); Matcher m = p.matcher(“aaaaab”); boolean b = m.matches();

      A matches method is defined by this class as a convenience for when a regular expression is used just once. This method compiles an expression and matches an input sequence against it in a single invocation. The statement ‘boolean b = Pattern.matches(“a*b”, “aaaaab”);’ is equivalent to the three statements above, though for repeated matches it is less efficient since it does not allow the compiled pattern to be reused. Instances of this class are immutable and are safe for use by multiple concurrent threads. Instances of the Matcher class are not safe for such use.”[JavaAPI]

  • Matcher.matches() vs Matcher.find() vs Matcher.lookingAt()
    “An engine that performs match operations on a character sequence by interpreting a Pattern. A matcher is created from a pattern by invoking the pattern’s matcher method. Once created, a matcher can be used to perform three different kinds of match operations:

    • The matches method attempts to match the entire input sequence against the pattern.
    • The lookingAt method attempts to match the input sequence, starting at the beginning, against the pattern.It does not require that the entire region be matched.If the match succeeds then more information can be obtained via the start, end, and group methods.
    • The find method scans the input sequence looking for the next subsequence that matches the pattern.

    Each of these methods returns a boolean indicating success or failure. More information about a successful match can be obtained by querying the state of the matcher.”[JavaAPI]

+ cf. Matcher.find() vs Matcher.matches()
“ matches() will only return true if the full string is matched. find() will try to find the next occurrence within the substring that matches the regex. Note the emphasis on “the next”. That means, the result of calling find() multiple times might not be the same. In addition, by using find() you can call start() to return the position the substring was matched. So, be careful when calling find() multiple times if the Matcher object was not reset, even when the regex is surrounded with ^ and $ to match the full string.”

            final Matcher subMatcher = Pattern.compile("\\d+").matcher("skrf35kesruytfkwu4ty7sdfs");
            System.out.println("Found: " + subMatcher.matches());
            System.out.println("Found: " + subMatcher.find() + " - position " + subMatcher.start());
            System.out.println("Found: " + subMatcher.find() + " - position " + subMatcher.start());
            System.out.println("Found: " + subMatcher.find() + " - position " + subMatcher.start());
            System.out.println("Found: " + subMatcher.find());
            System.out.println("Found: " + subMatcher.find());
            System.out.println("Matched: " + subMatcher.matches());

            final Matcher fullMatcher = Pattern.compile("^\\w+$").matcher("skrf35kesruytfkwu4ty7sdfs");
            System.out.println("Found: " + fullMatcher.find() + " - position " + fullMatcher.start());
            System.out.println("Found: " + fullMatcher.find());
            System.out.println("Found: " + fullMatcher.find());
            System.out.println("Matched: " + fullMatcher.matches());
            System.out.println("Matched: " + fullMatcher.matches());
            System.out.println("Matched: " + fullMatcher.matches());
            System.out.println("Matched: " + fullMatcher.matches());
            Found: false
            Found: true - position 4
            Found: true - position 17
            Found: true - position 20
            Found: false
            Found: false
            Matched: false
            Found: true - position 0
            Found: false
            Found: false
            Matched: true
            Matched: true
            Matched: true
            Matched: true

Scanner with Pattern

cf. JavaAPI - java.util.Scanner Scanner.class

String regExp = "\\d{1,4}[-\\s]\\d{1,4}[-\\s]\\d{4,10}";
String input = "148-809-2561957985";
  1. scanner.useDelimiter()
      Scanner s = new Scanner(input).useDelimiter("\\d{1,4}[-\\s]\\d{1,4}[-\\s]\\d{4,10}");
  2. scanner.findInLine()
         Scanner sc = new Scanner(input);
         MatchResult result = sc.match();
         for(int i = 1; i<=result.groupCount(); i++){
  3. scanner.next()
     Scanner sc = new Scanner(input);
     System.out.println("Senetense:" sc.next(regExp));
Written on July 11, 2017