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

6 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 zero or more times)
        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 zero or more times)
        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 zero or more times)
        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 "aa" from index 0 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 "aa" from index 0 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 zero or more times)
        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 zero or more times)
        searchREGEX("baa", "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 "aa" from index 1 to 3
a*: Found "" from index 3 to 3

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 3 to 3

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

 



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 zero or more times)
        searchREGEX("baaba", "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 "aa" from index 1 to 3
a*: Found "" from index 3 to 3
a*: Found "a" from index 4 to 5
a*: Found "" from index 5 to 5

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 3 to 3
a*?: Found "" from index 4 to 4
a*?: Found "" from index 5 to 5

a*+: Found "" from index 0 to 0
a*+: Found "aa" from index 1 to 3
a*+: Found "" from index 3 to 3
a*+: Found "a" from index 4 to 5
a*+: Found "" from index 5 to 5
  
*/

 



answered Jan 20, 2016 by avibootz

Related questions

9 answers 780 views
5 answers 1,046 views
5 answers 446 views
1 answer 195 views
1 answer 175 views
...