Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
BlueHost Web Hosting

Bonsai Boy of New York Bonsai Trees and Accessories

Liquid Web iThemes WordPress Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

DreamHost Web Hosting


getResponse autofunnels

Liquid Web Cloud VPS Hosting

Disclosure We are a professional Information website that receives compensation from some of the links whose we show you with Information. It means that my content may contain affiliate links.

12,168 questions

16,572 answers

573 users

How to use X{n} , X{n}? and X{n}+ regular expressions (REGEX) in Java

9 Answers

0 votes
package javaapplication1;
  
import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
public class Example {
    public static void main(String[] args) {
        // X{n} , X{n}? and X{n}+  (X exactly n times)
        searchREGEX("", "a{1}", "a{1}?", "a{1}+");
    }
      
    private static void searchREGEX(String input, String... regex_list) {
        for (String regex : regex_list) {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(input);
   
            boolean found = false;
            while (matcher.find()) {
               System.out.println(String.format("%s: Found" + " \"%s\" " + "from index %d to %d",
                                       regex, matcher.group(), matcher.start(), matcher.end()));
               found = true;
            }
            System.out.println();
            if (!found) 
                System.out.println("Not found");
        }
    }
}
  
  
/*
run:
   
Not found

Not found

Not found
   
*/

 




answered Jan 21, 2016 by avibootz
0 votes
package javaapplication1;
  
import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
public class Example {
    public static void main(String[] args) {
        // X{n} , X{n}? and X{n}+  (X exactly n times)
        searchREGEX("a", "a{1}", "a{1}?", "a{1}+");
    }
      
    private static void searchREGEX(String input, String... regex_list) {
        for (String regex : regex_list) {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(input);
   
            boolean found = false;
            while (matcher.find()) {
               System.out.println(String.format("%s: Found" + " \"%s\" " + "from index %d to %d",
                                       regex, matcher.group(), matcher.start(), matcher.end()));
               found = true;
            }
            System.out.println();
            if (!found) 
                System.out.println("Not found");
        }
    }
}
  
  
/*
run:
   
a{1}: Found "a" from index 0 to 1

a{1}?: Found "a" from index 0 to 1

a{1}+: Found "a" from index 0 to 1
   
*/

 




answered Jan 21, 2016 by avibootz
0 votes
package javaapplication1;
  
import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
public class Example {
    public static void main(String[] args) {
        // X{n} , X{n}? and X{n}+  (X exactly n times)
        searchREGEX("aa", "a{1}", "a{1}?", "a{1}+");
    }
      
    private static void searchREGEX(String input, String... regex_list) {
        for (String regex : regex_list) {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(input);
   
            boolean found = false;
            while (matcher.find()) {
               System.out.println(String.format("%s: Found" + " \"%s\" " + "from index %d to %d",
                                       regex, matcher.group(), matcher.start(), matcher.end()));
               found = true;
            }
            System.out.println();
            if (!found) 
                System.out.println("Not found");
        }
    }
}
  
  
/*
run:
   
a{1}: Found "a" from index 0 to 1
a{1}: Found "a" from index 1 to 2

a{1}?: Found "a" from index 0 to 1
a{1}?: Found "a" from index 1 to 2

a{1}+: Found "a" from index 0 to 1
a{1}+: Found "a" from index 1 to 2
   
*/

 




answered Jan 21, 2016 by avibootz
0 votes
package javaapplication1;
  
import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
public class Example {
    public static void main(String[] args) {
        // X{n} , X{n}? and X{n}+  (X exactly n times)
        searchREGEX("aba", "a{1}", "a{1}?", "a{1}+");
    }
      
    private static void searchREGEX(String input, String... regex_list) {
        for (String regex : regex_list) {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(input);
   
            boolean found = false;
            while (matcher.find()) {
               System.out.println(String.format("%s: Found" + " \"%s\" " + "from index %d to %d",
                                       regex, matcher.group(), matcher.start(), matcher.end()));
               found = true;
            }
            System.out.println();
            if (!found) 
                System.out.println("Not found");
        }
    }
}
  
  
/*
run:
   
a{1}: Found "a" from index 0 to 1
a{1}: Found "a" from index 2 to 3

a{1}?: Found "a" from index 0 to 1
a{1}?: Found "a" from index 2 to 3

a{1}+: Found "a" from index 0 to 1
a{1}+: Found "a" from index 2 to 3
   
*/

 




answered Jan 21, 2016 by avibootz
0 votes
package javaapplication1;
  
import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
public class Example {
    public static void main(String[] args) {
        // X{n} , X{n}? and X{n}+  (X exactly n times)
        searchREGEX("a", "a{2}", "a{2}?", "a{2}+");
    }
      
    private static void searchREGEX(String input, String... regex_list) {
        for (String regex : regex_list) {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(input);
   
            boolean found = false;
            while (matcher.find()) {
               System.out.println(String.format("%s: Found" + " \"%s\" " + "from index %d to %d",
                                       regex, matcher.group(), matcher.start(), matcher.end()));
               found = true;
            }
            System.out.println();
            if (!found) 
                System.out.println("Not found");
        }
    }
}
  
  
/*
run:
   
Not found

Not found

Not found
   
*/

 




answered Jan 21, 2016 by avibootz
0 votes
package javaapplication1;
  
import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
public class Example {
    public static void main(String[] args) {
        // X{n} , X{n}? and X{n}+  (X exactly n times)
        searchREGEX("aba", "a{2}", "a{2}?", "a{2}+");
    }
      
    private static void searchREGEX(String input, String... regex_list) {
        for (String regex : regex_list) {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(input);
   
            boolean found = false;
            while (matcher.find()) {
               System.out.println(String.format("%s: Found" + " \"%s\" " + "from index %d to %d",
                                       regex, matcher.group(), matcher.start(), matcher.end()));
               found = true;
            }
            System.out.println();
            if (!found) 
                System.out.println("Not found");
        }
    }
}
  
  
/*
run:
   
Not found

Not found

Not found
   
*/

 




answered Jan 21, 2016 by avibootz
edited Feb 26, 2016 by avibootz
0 votes
package javaapplication1;
  
import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
public class Example {
    public static void main(String[] args) {
        // X{n} , X{n}? and X{n}+  (X exactly n times)
        searchREGEX("aababaa", "a{2}", "a{2}?", "a{2}+");
    }
      
    private static void searchREGEX(String input, String... regex_list) {
        for (String regex : regex_list) {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(input);
   
            boolean found = false;
            while (matcher.find()) {
               System.out.println(String.format("%s: Found" + " \"%s\" " + "from index %d to %d",
                                       regex, matcher.group(), matcher.start(), matcher.end()));
               found = true;
            }
            System.out.println();
            if (!found) 
                System.out.println("Not found");
        }
    }
}
  
  
/*
run:
   
a{2}: Found "aa" from index 0 to 2
a{2}: Found "aa" from index 5 to 7

a{2}?: Found "aa" from index 0 to 2
a{2}?: Found "aa" from index 5 to 7

a{2}+: Found "aa" from index 0 to 2
a{2}+: Found "aa" from index 5 to 7
   
*/

 




answered Jan 21, 2016 by avibootz
0 votes
package javaapplication1;
   
import java.util.regex.Matcher;
import java.util.regex.Pattern;
   
public class Example {
    public static void main(String[] args) {
        // X{n} , X{n}? and X{n}+  (X exactly n times)
        searchREGEX("catcatcatcatcat", "cat{3}", "(cat){3}", "(cat){6}", "[cat]{3}");
    }
       
    private static void searchREGEX(String input, String... regex_list) {
        for (String regex : regex_list) {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(input);
    
            boolean found = false;
            while (matcher.find()) {
               System.out.println(String.format("%s: Found" + " \"%s\" " + "from index %d to %d",
                                       regex, matcher.group(), matcher.start(), matcher.end()));
               found = true;
            }
            System.out.println();
            if (!found) 
                System.out.println(String.format("%s: Not found\n", regex));
        }
    }
}
   
   
/*
run:
    
cat{3}: Not found

(cat){3}: Found "catcatcat" from index 0 to 9


(cat){6}: Not found

[cat]{3}: Found "cat" from index 0 to 3
[cat]{3}: Found "cat" from index 3 to 6
[cat]{3}: Found "cat" from index 6 to 9
[cat]{3}: Found "cat" from index 9 to 12
[cat]{3}: Found "cat" from index 12 to 15
    
*/

 




answered Jan 22, 2016 by avibootz
0 votes
package javaapplication1;
   
import java.util.regex.Matcher;
import java.util.regex.Pattern;
   
public class Example {
    public static void main(String[] args) {
        // X{n} , X{n}? and X{n}+  (X exactly n times)
        searchREGEX("catcatcatcatcat", "(cat){3}", "(cat){3}?", "(cat){3}+");
        searchREGEX("catcatcatcatcat", "[cat]{3}", "[cat]{3}?", "[cat]{3}+");
    }
       
    private static void searchREGEX(String input, String... regex_list) {
        for (String regex : regex_list) {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(input);
    
            boolean found = false;
            while (matcher.find()) {
               System.out.println(String.format("%s: Found" + " \"%s\" " + "from index %d to %d",
                                       regex, matcher.group(), matcher.start(), matcher.end()));
               found = true;
            }
            System.out.println();
            if (!found) 
                System.out.println(String.format("%s: Not found\n", regex));
        }
    }
}
   
   
/*
run:
    
(cat){3}: Found "catcatcat" from index 0 to 9

(cat){3}?: Found "catcatcat" from index 0 to 9

(cat){3}+: Found "catcatcat" from index 0 to 9

[cat]{3}: Found "cat" from index 0 to 3
[cat]{3}: Found "cat" from index 3 to 6
[cat]{3}: Found "cat" from index 6 to 9
[cat]{3}: Found "cat" from index 9 to 12
[cat]{3}: Found "cat" from index 12 to 15

[cat]{3}?: Found "cat" from index 0 to 3
[cat]{3}?: Found "cat" from index 3 to 6
[cat]{3}?: Found "cat" from index 6 to 9
[cat]{3}?: Found "cat" from index 9 to 12
[cat]{3}?: Found "cat" from index 12 to 15

[cat]{3}+: Found "cat" from index 0 to 3
[cat]{3}+: Found "cat" from index 3 to 6
[cat]{3}+: Found "cat" from index 6 to 9
[cat]{3}+: Found "cat" from index 9 to 12
[cat]{3}+: Found "cat" from index 12 to 15
    
*/

 




answered Jan 22, 2016 by avibootz
...