PROGRAMMING

m9/ JAVA
REMEMBERS




Last update:   19-10-2021

Matches

! The Pattern matches() returns true only when preciseley matches the pattern.
 
/**
 * 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
        );
    }
}

... 13 lines
 

GREEDY

! Greedy behavior (default) matches the longest sequence.
 
/**
 * In the followingcode you might have expect two matches: 
 * "extend" and "end"
 * 
 * This is call GREEDY behavior ...
 * which 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) {

        // greedy
        Pattern p = Pattern.compile("e.+d");
        Matcher m = p.matcher("extend cup end table");

        while(m.find()) {
            System.out.println(m.group());
                // extend cup end
        }

        // ungreedy
        Pattern p2 = Pattern.compile("e.+?d"); // Look Here
        Matcher m2 = p2.matcher("extend cup end table");

        while(m2.find()) {
            System.out.println(m2.group());
                // extend
                // end
        }
    }
}

... 24 lines
 

Replace

You can 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");
            // Match: abc, (zero or more), ungreedy, space

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

... 12 lines
 

Split

With the split() method you can get the text found on either side of the pattern.
 
/**
 * Use Pattern split() to break the string in parts ...
 * using regex pattern 
 */

package com.minte9.basics.regexp;

import java.util.regex.Pattern;

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

        /**
         * Split by char
         */
        Pattern p = Pattern.compile("[ ,.!]");
        String ss[] = p.split("one two,alpha9 12!done.");
        for (String s : ss) {
            System.out.println(s);
            // one
            // two
            // alpha9
            // 12
            // done
        }

        /**
         * Split by camel case
         */
        String s = "AbCdEf";
        String[] words = s.split("(?=[A-Z])"); // Look Ahead
        for(String word:words) {
            System.out.println(word);
                // Ab
                // Cd
                // Ef
        }
    }
}

... 27 lines
 

Lookaround

! Lookarounds are not included in the match.
 
package com.minte9.basics.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Lookaround {  

    public static void main(String[] args) {

        // Look Behind
        Pattern p = Pattern.compile("(?<=a)xyz");
        Matcher m = p.matcher("axyz");
        System.out.println(m.find()); // true

        // Look Ahead
        Pattern p2 = Pattern.compile("(?=x)xyz");
        Matcher m2 = p2.matcher("axyz");
        System.out.println(m2.find()); // true
    }
}
... 9 lines
 

Questions     github Basics
Inheritance

        A B C D E F
🔔
1/3