Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,913 questions

51,846 answers

573 users

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

5 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? , X?? and X?+  (X once or not at all)
        searchREGEX("", "a?", "a??", "a?+");
    }
     
    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?: Found "" from index 0 to 0

a??: Found "" from index 0 to 0

a?+: Found "" from index 0 to 0
  
*/

 



answered Jan 20, 2016 by avibootz
edited Jan 20, 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? , X?? and X?+  (X once or not at all)
        searchREGEX("a", "a?", "a??", "a?+");
    }
     
    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?: Found "a" from index 0 to 1
a?: Found "" from index 1 to 1

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

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

 



answered Jan 20, 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? , X?? and X?+  (X once or not at all)
        searchREGEX("aa", "a?", "a??", "a?+");
    }
   	
    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?: Found "a" from index 0 to 1
a?: Found "a" from index 1 to 2
a?: Found "" from index 2 to 2

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

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

 



answered Jan 20, 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? , X?? and X?+  (X once or not at all)
        searchREGEX("ba", "a?", "a??", "a?+");
    }
     
    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?: Found "" from index 0 to 0
a?: Found "a" from index 1 to 2
a?: Found "" from index 2 to 2

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

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

 



answered Jan 20, 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? , X?? and X?+  (X once or not at all)
        searchREGEX("b", "a?", "a??", "a?+");
    }
      
    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?: Found "" from index 0 to 0
a?: Found "" from index 1 to 1

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

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

 



answered Jan 20, 2016 by avibootz

Related questions

9 answers 706 views
5 answers 1,010 views
6 answers 451 views
1 answer 183 views
1 answer 154 views
...