Matching Specific String

Sort by

recency

|

67 Discussions

|

  • + 0 comments

    \w{3}\W\w{10}\W\w{3}

  • + 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 sc = new Scanner(System.in);
            Pattern pattern = Pattern.compile("hackerrank");
            int count = 0;
            while(sc.hasNext())
            {   Matcher matcher = pattern.matcher(sc.next());
                if (matcher.find()) count++;
            }
            System.out.println("Number of matches : "+count);
        }
    }
    
  • + 0 comments

    java solution

    import java.io.*;
    import java.util.*;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            Pattern pattern = Pattern.compile("hackerrank");
            int count = 0;
            while (scanner.hasNext()) {
                Matcher matcher = pattern.matcher(scanner.next());
                if (matcher.find()) count++;
            }
            System.out.println("Number of matches : " + count);
        }
    }
    
  • + 0 comments

    This got me confused, I thought what the task wants is to search characters between the "." in example URL. so I used this regex "(?<=.).(?=.)" however it seems it only requires to search for "hackerrank" string only. My reading comprehension is too rusty.

  • + 0 comments
    #include "bits/stdc++.h"
    using namespace std;
    
    
    int main() {
        string s;
        getline(cin,s);
        regex r("hackerrank");
        if(regex_search(s,r)){
            auto word_begin=sregex_iterator(s.begin(),s.end(),r);
            auto word_end=sregex_iterator();
            cout<<"Number of matches : "<<distance(word_begin, word_end);
        }
        return 0;
    }