minte9
LearnRemember



Pattern / True

The Pattern matches() returns true only when preciseley matches the pattern.
 
/**
 * Pattern, Matcher
 * 
 * The Pattern matches() if all the string match the pattern
 * To check only parts of the string find() is used
 */

package com.minte9.basics.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Matches {
    public static void main(String[] args) {

        Pattern p = Pattern.compile("Java");
        Matcher m = p.matcher("Java SE 8");

        System.out.println(m.matches()); // false 
        System.out.println(m.find());    // true
    }
}

Greedy / Longest

Greedy behavior (default) matches the longest sequence.
 
/**
 * Greedy, Ungreedy
 * 
 * Greedy behavior match the longest sequence
 * Use "?" modifier for ungreedy behavior
 */

package com.minte9.basics.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Greedy {
    public static void main(String[] args) {

        Pattern p;
        Matcher m;

        p = Pattern.compile("e.+d"); // Greedy
        m = p.matcher("extend cup end table");
        while(m.find()) {
            System.out.println(m.group()); // extend cup end
        }

        p = Pattern.compile("e.+?d"); // Ungreedy
        m = p.matcher("extend cup end table");
        while(m.find()) {
            System.out.println(m.group()); // extend, end
        }

        p = Pattern.compile("Java ?(8|SE)"); // Ungreedy
        m = p.matcher("Java 8 Java SE");
        while(m.find()) {
            System.out.println(m.group()); // Java 8 , Java SE
        }
    }
}

Replace

Replace every sequence that matches the pattern with the replacement string.
 
/**
 * Matcher replaceAll()
 * 
 * Replace the matches from pattern with the provided string
 */

package com.minte9.basics.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Replace {
    public static void main(String[] args) {

        Pattern p = Pattern.compile("abc.*? ");
        Matcher m = p.matcher("abc abcd bcd cef");
            // abc, char (zero or more), ungreedy, space

        while(m.find()) {
            String r = m.replaceAll("AAA "); // Look Here
            System.out.println(r); // AAA AAA bcd cef
        }
    }
}

Split

Get the text found on either side of the pattern.
 
/**
 * Pattern, String split()
 * Break the string in parts using regex pattern 
 * Split by char
 * Split by camer case
 */

package com.minte9.basics.regexp;
import java.util.regex.Pattern;

public class Split {
    public static void main(String[] args) {

        String[] words;

        Pattern p = Pattern.compile("[ ,.!]");
        words = p.split("one two,alpha9 12!done.");
        for (String w:words) {
            System.out.println(w); // one two alpha9 12 done
        }

        words = "AbCdEf".split("(?=[A-Z])"); // Look Ahead
        for(String w:words) {
            System.out.println(w); // Ab Cd Ef
        }
    }
}

Lookaround

Lookarounds are not included in the match.
T 
package com.minte9.basics.regexp;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Test;

public class LookaroundTest {  

    public static boolean find(String regex, String str) {
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        return m.find();
    } 
    
    @Test public void lookBehind() {
        assertTrue(find("(?<=a)x", "axyz"));  // Look behind for a (true)
        assertFalse(find("(?<=b)x", "axyz")); // Look befind for a (false)
    }

    @Test public void lookAhead() {
        assertTrue(find("(?=x)xyz", "axyz")); // Look ahead for x (true)
        assertFalse(find("(?=x)ax", "axyz")); // Look ahead for x (false)
    } 

    @Test public void lookBehindNegative() {
        assertFalse(find("(!?<=a)x", "ax"));  // Look behind for !a (false)
    }

    @Test public void lookAheadNegative() {
        assertFalse(find("(?!x)x", "ax"));    // Look ahead for !x (false)
    }
}



  Last update: 382 days ago