Sort by

recency

|

66 Discussions

|

  • + 0 comments

    2 Valid Patterns

    The literal pattern is simply:

    \okokok\
    

    However, the description leaves a clue for using a non-capture group to help with this pattern.

    The non-capture group pattern is:

    \(?:ok){3}\
    

    Testing against 6 "ok" values

    When testing the string input okokokokokok against these 2 patterns, the same 2 matches are returned:

    Matches Returned:

     - matches\[0\] okokok
     - matches\[1\] okokok
    

    Regarding Issues with other Patterns

    I noticed a few submissions that return true on matches, but they either produce the wrong number of matches or invalid match values.

    Invalid Pattern Returns 4 Matches instead of 2:

    \(ok){3}\

    Matches Returned:

     - matches\[0\] okokok
     - matches\[1\] ok
     - matches\[2\] okokok
     - matches\[3\] ok
    

    Invalid Pattern Returns wrong values:

    \(ok){3,}\

    Matches Returned:

     - matches\[0\] okokokokokok
     - matches\[1\] ok
    
  • + 0 comments
    Regex_Pattern = r'(ok){3,}'
    
    import re
    
    print(str(bool(re.search(Regex_Pattern, input()))).lower())
    
  • + 0 comments

    Regex_Pattern = r'(ok){3,}' # Do not delete 'r'.

  • + 0 comments

    Java 15:

    import java.io.*;
    import java.util.*;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    public class Solution {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            // Pattern pattern = Pattern.compile("(ok){3,}");   //WORKS
            Pattern pattern = Pattern.compile("(ok)\\1\\1");    
            // \\1 means anything in first captured-group. Here ok is in first ()
            Matcher matcher = pattern.matcher(scanner.nextLine());
            System.out.println(matcher.find()); 
        }
    }
    
  • + 0 comments
    (ok){3,}