Pages

Wednesday, 23 January 2013

Regex - Quantifiers

Quantifiers:
? - zero or one
* - zero or more
+ - one ore more

Regex - Regular Expressions. This is one of the difficult topics for me. Here is one program to show work with quantifiers.


/*
 * Regex - Quantifiers. Link for API:
http://docs.oracle.com/javase/tutorial/essential/regex/quant.html
 * 
 */
package pattern_matcher_test_01;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Alex
 */
public class Quantifiers {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        FirstTest();
        SecondTest();
        PlusTest();
        DotTest();
        GreedyDotTest();
        DotStarTest();
    }
    
    public static void FirstTest() {
       Pattern p = Pattern.compile("ab"); //a, followed by b
       Matcher m = p.matcher("_abab1abb2abba3aab");
       int i = 0;
       while (m.find()){
           System.out.println("\"ab\" found: " + m.start() +" " + m.group());
           i++;
       }
       System.out.println("found: " + i + " time(s)" + "\n");
    }
    
    public static void SecondTest() {
       Pattern p = Pattern.compile("ab+"); //a, followed by b one or more times (It would not mean "ab" one or more times)
       Matcher m = p.matcher("_abab1abb2abba3aab");
       int i = 0;
       while (m.find()){
           System.out.println("\"ab+\" found: " + m.start() +" " + m.group());
           i++;
       }
       System.out.println("found: " + i + " time(s)" + "\n");
    }
    
    public static void DotTest() {
       Pattern p = Pattern.compile("ab."); //a, followed by b, followed by any character
       Matcher m = p.matcher("_abab1abb2abba3aab");
       int i = 0;
       while (m.find()){
           System.out.println("\"ab.\" found: " + m.start() +" " + m.group());
           i++;
       }
       System.out.println("found: " + i + " time(s)" + "\n");
    }
  
    public static void PlusTest() {
       Pattern p = Pattern.compile("(ab)+"); //"ab" one or more times
       Matcher m = p.matcher("_abab1abb2abba3aab");
       int i = 0;
       while (m.find()){
           System.out.println("\"(ab)+\" found: " + m.start() +" " + m.group());
           i++;
       }
       System.out.println("found: " + i + " time(s)" + "\n");
    }
    
    public static void GreedyDotTest() {
       Pattern p = Pattern.compile("ab.*"); //ab, followed by "anything", zero or more times
       Matcher m = p.matcher("_abab1abb2abba3aab");
       int i = 0;
       while (m.find()){
           System.out.println("\"ab.*\" found: " + m.start() +" " + m.group());
           i++;
       }
       System.out.println("found: " + i + " time(s)" + "\n");
    }
    
    public static void DotStarTest() {
       Pattern p = Pattern.compile("a.*?b"); //a, followed by "anything", zero or more times, followed by b
       Matcher m = p.matcher("_abab1abb2abba3aab");
       int i = 0;
       while (m.find()){
           System.out.println("\"a.*?b\" found: " + m.start() +" " + m.group());
           i++;
       }
       System.out.println("found: " + i + " time(s)" + "\n");
    }
}

This produces output:
"ab" found: 1 ab
"ab" found: 3 ab
"ab" found: 6 ab
"ab" found: 10 ab
"ab" found: 16 ab
found: 5 time(s)

"ab+" found: 1 ab
"ab+" found: 3 ab
"ab+" found: 6 abb
"ab+" found: 10 abb
"ab+" found: 16 ab
found: 5 time(s)

"(ab)+" found: 1 abab
"(ab)+" found: 6 ab
"(ab)+" found: 10 ab
"(ab)+" found: 16 ab
found: 4 time(s)

"ab." found: 1 aba
"ab." found: 6 abb
"ab." found: 10 abb
found: 3 time(s)

"ab.*" found: 1 abab1abb2abba3aab
found: 1 time(s)

"a.*?b" found: 1 ab
"a.*?b" found: 3 ab
"a.*?b" found: 6 ab
"a.*?b" found: 10 ab
"a.*?b" found: 13 a3aab
found: 5 time(s)

No comments:

Post a Comment