Regex in java , part two , The Matcher class

The Pattern has some static methods such as matcher which allow us to match a regex against a string from its start till its end , and quote which allow us to quote or escape a regex String . This class has also some static method , which allow us to compile a regex string into an instance of the Pattern class . The instance can be used to split a String , and it can also be used to get an instance of the Matcher class .

The Matcher class has some methods that can be grouped into :

  • region methods , used to set the region where matching is done .
  • Matching methods , used to find the matches
  • information methods , to get information about the found matches , these methods are used with the matching methods .
  • append methods used with the matching methods
  • reset methods , to reset the Matching region , and the last found match
  • replace methods , used to replace the found match by a replacement string .
  • Other methods

Getting an instance of the matcher class

we can create an instance of the Matcher class by using the matcher method of an instance of the Pattern class.

import java.util.regex.Pattern;
import java.util.regex.Matcher;
import static java.lang.System.out;

class MatcherInstance {

    public static void matcherInstance() {

        Pattern aPattern;
        Matcher aMatcher;
        aPattern = Pattern.compile("a regex");
        // create a pattern

        aMatcher = aPattern.matcher("String to match");
        // create an instance of the Matcher class using
        // The Pattern instance macher method passing to it a string to match

        out.println("Created matcher is : " + aMatcher);
        // Created matcher is : java.util.regex.Matcher[pattern=a regex region=0,15
        // lastmatch=]

    }

    public static void main(String[] args) {
        matcherInstance();

    }

}

region methods

Matcher	region(int start, int end)

The matcher region is the region in the char sequence in which we are going to perform matching . so it can be from the start till the end of the char sequence , or we can specify the start (inclusive) and the end (exclusive) using the region method . This will affect only matching methods .

int	regionEnd()
int	regionStart()

we can use the regionEnd , and regionStart method to get the start and end position of the matching .

Matcher	useAnchoringBounds(boolean b)

When the start and end region is set , and we want them to act as anchoring regions so the start of the region will be matched by the ^ meta character , and the end of the region will be matched by the $ meta character , we can use the useAnchoringBounds method . if we pass true to this method , this means that the region bounds will act as anchoring bounds , and if we pass false , this means they will not .

boolean	hasAnchoringBounds()

we can use the hasAnchoringBounds method to check if the region bounds act as anchoring bounds . it will return true if yes , false if no .

Matcher	useTransparentBounds(boolean b)

When creating a regex , we can use look ahead and look behind modifier to check if there is a pattern before or after the regex we are trying to match , without capturing this pattern . if we pass true to this method , it will allow the look ahead and look behind modifier to see beyond the set boundaries , if we pass false , it will not allow them to see beyond the boundaries .

import java.util.regex.Pattern;
import java.util.regex.Matcher;
import static java.lang.System.out;

class MatcherRegion {

    public static void matcherRegion() {

        Pattern aPattern = Pattern.compile("^ab$");
        // create a pattern using regex

        Matcher aMatcher = aPattern.matcher("12ab31");
        // create the matcher class

        out.println(aMatcher.regionStart()); // 0
        // get region start
        // output : 0

        out.println(aMatcher.regionEnd()); // 6
        // get region end
        // output : 6

        out.println(aMatcher.hasAnchoringBounds());
        // check if we have anchoring bound
        // output : true

        out.println(aMatcher.hasTransparentBounds());
        // check if we have transparent bound
        // output : false

        aMatcher.region(5, 6);
        // set region end , start

        out.println(aMatcher.regionStart());
        // get region start
        // output : 5

        out.println(aMatcher.regionEnd());
        // get region end
        // output : 6

        out.println(aMatcher.hasAnchoringBounds());
        // check if we have anchoring bound
        // true

        out.println(aMatcher.hasTransparentBounds());
        // check if we have transparent bound
       // false

        aMatcher.useAnchoringBounds(false);
        // set the anchoring bound to false

        aMatcher.useTransparentBounds(true);
        // set the transparent bound to true

        out.println(aMatcher.hasAnchoringBounds());
        // check if we have anchoring bound
        // false

        out.println(aMatcher.hasTransparentBounds());
        // check if we have transparent bound
        // true

    }

    public static void main(String[] args) {
        matcherRegion();

    }

}

By default when we create an instance of the Matcher class , the anchoring Bounds is set to True , and the transparent bounds is set to false . When we set the region to match , this doesn’t affect the anchoring and transparent bounds , we must set them using useAnchoringBounds and useAnchoringBounds.

Matching methods

The matching methods allow us to find matches in the String using the regex that is specified . They will also allow us to use other methods like for example to get information about the match that is found.

we can group the matching methods in two :

  • Those that will find a match not based on the previous one :
    • boolean find(int start)
    • boolean matches()
    • boolean lookingAt()
  • those that continue finding a matching based on the previous one :
    • boolean find()

boolean matches()

public boolean matches()

The matches method will match a regex against a String from the start of the set region till its end . So the regex , must match the string from its start till its end . We can use other Matcher class instance methods with the resulting match , to get information about it or to do append and replacement .

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.System.out;

class MatcherMatches {

    public static void matcherMatches() {

        Pattern aPattern;
        Matcher aMatcher;
        String aString = "cdaa";

        aPattern = Pattern.compile("(a+)");
        // create a pattern which match an a one or more times

        aMatcher = aPattern.matcher(aString);
        // get a Matcher instance from the Pattern instance

        out.println(aMatcher.matches());
        // a+ doesn't match cdaa from its start till its end , hence it fails
        // output : false

        aMatcher.region(2, aString.length());
        out.println(aMatcher.matches());
        // the region to match now is aa
        // a+ match the region from start till its end
        // output : true

        out.println("Match found at  position : " + aMatcher.start());
        // get the start position of the found match
        // output : Match found at position : 2

        out.println("Match found end position : " + aMatcher.end());
        // get the end position of the found match
        // output : Match found at position : 4

        out.println("Found Match is : " + aMatcher.group());
        // get the found match using group method
        // output : Found Match is : aa

        out.println("group count : " + aMatcher.groupCount());
        // the number of groups that we have captured using ()
        // group 0 is the whole match , and group 1 is the first capture group
        // group count : 1

        out.println("group 1 match starting position : " + aMatcher.start(1));
        // the start position of the first found group
        // group 1 match starting position : 2

        out.println("group 1 match end position : " + aMatcher.end(1));
        // the end position of the first found group
        // group 1 match end position : 4

        out.println("Found Match is : " + aMatcher.group(1));
        // get the first captured group
        // output : Found Match is : aa

        out.println("Found Match hit end : " + aMatcher.hitEnd());
        // check if the match that is found hit the end anchor
        // output : Found Match hit end : true

        out.println("Found Match requires ends : " + aMatcher.requireEnd());
        // the regex end requires anchoring bounds
        // output : Found Match requires ends : false

        StringBuffer stringReplaced = new StringBuffer();
        aMatcher.appendReplacement(stringReplaced, "dd");
        // append a replacement of the match to the string found before the match

        out.println("String replaced " + stringReplaced);
        // String replaced cddd

    }

    public static void main(String[] args) {
        matcherMatches();
    }

}

boolean lookingAt()

public boolean lookingAt()

The lookingAt method will start matching from the start of the specified region. So the start of the region must match the regex , but it is not necessary for the end to match it .

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.System.out;

class MatcherLookingAt {

    public static void matcherLookingAt() {
        String aString = "11:22:33;22:11:00;these are the times that we have";
        Pattern aPattern;
        Matcher aMatcher;

        aPattern = Pattern.compile("(?<hour>\\d{1,2}):(?<minutes>\\d{2}):(?<seconds>\\d{2})");
        // regex to capture the hours , minutes and seconds
        // (?<name>regex) is used to name the capture group containing the regex
        // \d matches a digit
        // {1,2} : match at least 1 time and at most 2 times
        // {2} :match 2 times
        aMatcher = aPattern.matcher(aString);

        aMatcher.region(8, aString.length());
        // start matching from the ; after 33

        out.println(aMatcher.lookingAt());
        // lookingAt will start matching from the start of the region
        // the start of the region doesn't match the regex it is ; the
        // output : false

        aMatcher.region(9, aString.length());
        // start matching from the 2 after 33;

        out.println(aMatcher.lookingAt());
        // lookingAt will start matching from the start of the region
        // the region starts from 2 , the regex match so the
        // output : True

        // get the match information

        out.println("Match found at position : " + aMatcher.start());
        // aMatcher.start() : the found match start position
        // output : Match found at position : 9

        out.println("Match found end position : " + aMatcher.end());
        // aMatcher.end() : the found match end position
        // output : Match found end position : 17

        out.println("The match is : " + aMatcher.group());
        // aMatcher.group() : get the match found
        // output : The match is : 22:11:00

        out.println("The Hour is : " + aMatcher.group("hour"));
        // aMatcher.group("hours") : get the match group named hours and print it
        // output : The Hour is : 22

        out.println("The minutes are : " + aMatcher.group("minutes"));
        // aMatcher.group("minutes") : get the match group named minutes and print it
        // output : The minutes are : 11

        out.println("The seconds are : " + aMatcher.group("seconds"));
        // aMatcher.group("seconds") : get the match group named seconds and print it
        // output : The seconds are : 00

        out.println("Match hit end : " + aMatcher.hitEnd());
        // aMatcher.hitEnd() : True if the match that is found hit the end anchor
        // Match hit end : false

        out.println("Match require end : " + aMatcher.requireEnd());
        // aMatcher.requireEnd() : the regex end require anchoring bounds
        // Match require end : false

        StringBuffer stringBufferReplacement = new StringBuffer();
        aMatcher.appendReplacement(stringBufferReplacement, "_");
        // append the string before the match with "_"

        aMatcher.appendTail(stringBufferReplacement);
        // append the region after the match to stringBufferReplacement

        out.println("String replaced : " + stringBufferReplacement);
        // output : String replaced : 11:22:33;_;these are the times that we have

    }

    public static void main(String[] args) {
        matcherLookingAt();

    }
}

boolean find(int start)

The find(int start) method can be used to find a match anywhere in the string from the specified start position . we can get the information about the match ,and replace the found match .

import java.util.regex.Pattern;
import java.util.regex.Matcher;
import static java.lang.System.out;

class MatcherFindStart {

    public static void matcherFindStart() {
        Pattern aPattern;
        Matcher aMatcher;
        String aString = "11:22:33;22:11:00;these are the times that we have";

        aPattern = Pattern.compile("(?<hour>\\d{1,2}):(?<minutes>\\d{2}):(?<seconds>\\d{2})");
        // regex to capture the hours , minutes and seconds
        // (?<name>regex) is used to name the capture group containing the regex
        // \d matches a digit
        // {1,2} : match at least 1 time and at most 2 times
        // {2} :match 2 times

        aMatcher = aPattern.matcher(aString);
        // get the matcher from the pattern instance

        out.println(aMatcher.find(8));
        // find(int start) , will search for a match anywhere in the string starting
        // the specified postion , which is the ; after 33
        // output : True

        // get the match information

        out.println("Match found at position : " + aMatcher.start());
        // aMatcher.start() : the found match start position
        // output : Match found at position : 9

        out.println("Match found end position : " + aMatcher.end());
        // aMatcher.end() : the found match end position
        // output : Match found end position : 17

        out.println("The match is : " + aMatcher.group());
        // aMatcher.group() : get the found match
        // output : The match is : 22:11:00

        out.println("The Hour is : " + aMatcher.group("hour"));
        // aMatcher.group("hours") : get the match group named hours and print it
        // output : The Hour is : 22

        out.println("The minutes are : " + aMatcher.group("minutes"));
        // aMatcher.group("minutes") : get the match group named minutes and print it
        // output : The minutes are : 11

        out.println("The seconds are : " + aMatcher.group("seconds"));
        // aMatcher.group("seconds") : get the match group named seconds and print it
        // output : The seconds are : 00

        out.println("Match hit end : " + aMatcher.hitEnd());
        // aMatcher.hitEnd() : True if the match that is found hit the end anchor
        // Match hit end : false

        out.println("Match require end : " + aMatcher.requireEnd());
        // aMatcher.requireEnd() : True if the regex end require anchoring bounds ,
        // Match require end : false

        StringBuffer stringBufferReplacement = new StringBuffer();
        aMatcher.appendReplacement(stringBufferReplacement, "_");
        // append the string before the match with "_"

        aMatcher.appendTail(stringBufferReplacement);
        // append the region after the match to stringBufferReplacement

        out.println("String replaced : " + stringBufferReplacement);
        // output : String replaced : 11:22:33;_;these are the times that we have

    }

    public static void main(String[] args) {
        matcherFindStart();
    }

}

boolean find()

find() can be used to find a matches in a string .

  • If no matching methods is called before find , it will start searching , from the start of the set region .
  • If a matching methods is called before find , it will continue searching , after the last found match .
  • if called after replace methods find will return false
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import static java.lang.System.out;

class MatcherFind {

    public static void matcherFind() {
        String aStringOne = "11:22:33;22:11:00;";
        String aStringTwo = "1:22:33";
        Pattern aPattern;
        Matcher aMatcher;

        aPattern = Pattern.compile("(\\d{1,2}):(\\d{2}):(\\d{2})");
        // regex to capture the hours , minutes and seconds
        // \d matches a digit
        // {1,2} : match at least 1 time and at most 2 times
        // {2} :match 2 times

        aMatcher = aPattern.matcher(aStringOne); // "11:22:33;22:11:00;"

        out.println("aMatcher.matches() : " + aMatcher.matches());
        // matches(), match the string from its start till its end
        // regex (\\d{1,2}):(\\d{2}):(\\d{2}) doesn't match 11:22:33;22:11:00;
        // from its start till its end
        // output : aMatcher.matches() : false

        out.println("aMatcher.find() : " + aMatcher.find());
        // matches(), was called before find , find will continue
        // after the last found match , matches didn't find anything , so
        // find will start from the beginning , and as such it will
        // output : aMatcher.find() : true

        aMatcher = aPattern.matcher(aStringTwo);// "1:22:33"

        out.println("aMatcher.matches() : " + aMatcher.matches());
        // matches(), match the string from the start region till the end region
        // regex (\\d{1,2}):(\\d{2}):(\\d{2}) match 1:22:33
        // from its start till its end
        // output : aMatcher.matches() : true

        out.println("aMatcher.find() : " + aMatcher.find());
        // matches(), was called before find , find will continue
        // after the last found match , no matches after the last found match
        // output : aMatcher.find() : false

        aMatcher = aPattern.matcher(aStringOne);// "11:22:33;22:11:00;"

        out.println("aMatcher.lookingAt() : " + aMatcher.lookingAt());
        // lookingAt(), match the string from the start region
        // regex (\\d{1,2}):(\\d{2}):(\\d{2}) match 11:22:33;22:11:00;
        // from the start region
        // output : aMatcher.matches() : true

        out.println("found match is : " + aMatcher.group());
        // aMatcher.group() is used to print the matched string
        // output : 11:22:33

        out.println("aMatcher.find() : " + aMatcher.find());
        // matches(), was called before find , find will continue
        // after the last found match so from ; after 33
        // output : aMatcher.find() : true

        out.println("found match is : " + aMatcher.group());
        // aMatcher.group() is used to print the matched string
        // output : 22:11:00

        aMatcher = aPattern.matcher(aStringOne);// "11:22:33;22:11:00;"

        out.println("aMatcher.find(1) : " + aMatcher.find(1));
        // find(int start), look for a match in the string starting the specified
        // position
        // regex (\\d{1,2}):(\\d{2}):(\\d{2}) match 1:22:33;22:11:00;
        // output : aMatcher.matches() : true

        out.println("found match is : " + aMatcher.group());
        // aMatcher.group() is used to print the matched string
        // output : 1:22:33

        out.println("aMatcher.find() : " + aMatcher.find());
        // find(int start), was called before find , find will continue
        // after the last found match so from ; after 33
        // output : aMatcher.find() : true

        out.println("found match is : " + aMatcher.group());
        // aMatcher.group() is used to print the matched string
        // output : 22:11:00

        // get the match information

        out.println("Match found at position : " + aMatcher.start());
        // aMatcher.start() : the found match start position
        // output : Match found at position : 9

        out.println("Match found end position : " + aMatcher.end());
        // aMatcher.end() : the found match end position
        // output : Match found end position : 17

        out.println("Match hit end : " + aMatcher.hitEnd());
        // aMatcher.hitEnd() : True if the match that is found hit the end anchor
        // Match hit end : false

        out.println("Match require end : " + aMatcher.requireEnd());
        // aMatcher.requireEnd() : True if the regex end requires anchoring bounds
        // Match require end : false

        StringBuffer stringBufferReplacement = new StringBuffer();
        aMatcher.appendReplacement(stringBufferReplacement, "_");
        // append the string before the match with "_"

        aMatcher.appendTail(stringBufferReplacement);
        // append the region after the match to stringBufferReplacement

        out.println("String replaced : " + stringBufferReplacement);
        // output : String replaced : 11:22:33;_;

    }

    public static void main(String[] args) {
        matcherFind();
    }

}

information methods

String	group()
//return the match string 

int start()
//return the start position of the match

int end()
//return the end position of the match

int groupCount()
//return the count of the captured groups
// if no group were captured return 0 
// 0 is the whole match if any 
// if 1 group is captured return 1 
// 0 is the whole match , 1 is the first capture group

int start(int group) 
//return the matched group start position

int end(int group)
//return the matched group end position

String	group(int group)
//return the matched group string

int start(String name)
//return the matched named group start position 

int end(String name)
//return the matched named group end position 

String	group(String name)
//return the matched named group string 


//boolean hitEnd()
//return true if the match hit the end anchor $

boolean	requireEnd()
// return true if require end of region to match , so if more input is given 
// it will not match
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import static java.lang.System.out;

class MatcherInfo {

    public static void matcherInfo() {

        Pattern aPattern;
        Matcher aMatcher;

        aPattern = Pattern.compile("(\\d)(?<char>\\p{Alpha})");
        aMatcher = aPattern.matcher("1b");

        if (aMatcher.find()) {
            // if a match is found , get its information

            out.println("Found match is : " + aMatcher.group());
            // aMatcher.group() : return the match
            // output : Found match is : 1b

            out.println("Match start position : " + aMatcher.start());
            // aMatcher.start() : return the match start position
            // Match start position : 0

            out.println("Match end position : " + aMatcher.end());
            // aMatcher.end() : return the match end position
            // Match end position : 2

            out.println("Number of captured groups: " + aMatcher.groupCount());
            // aMatcher.groupCount() : return the number of captured group
            // Number of captured groups: 2

            // access the first capture group which is 1
            // 0 is the whole match

            out.println("Captured group 1 start position : " + aMatcher.start(1));
            // aMatcher.start(int group): return the capture group start position
            // Captured group 1 start position : 0

            out.println("Captured group 1 end position : " + aMatcher.end(1));
            // aMatcher.end(int group): return the capture group end position
            // Captured group 1 end position : 1

            out.println("Captured group 1 is : " + aMatcher.group(1));
            // aMatcher.group(int group): return the capture group string
            // Captured group 1 is : 1

            // access the second capture group which is 2 using its name
            // we can access also using 2

            out.println("Captured group 2 start position : " + aMatcher.start("char"));
            // aMatcher.start(string group): return the capture group start position
            // Captured group 2 start position : 1

            out.println("Captured group 2 end position : " + aMatcher.end("char"));
            // aMatcher.end(string group): return the capture group end position
            // Captured group 2 end position : 2

            out.println("Captured group 2 is : " + aMatcher.group("char"));
            // aMatcher.group(string group): return the capture group string
            // Captured group 2 is : b

            out.println("aMatcher.hitEnd() : " + aMatcher.hitEnd());
            // aMatcher.hitend(): return true if the match hit the end anchor
            // aMatcher.hitEnd() : false

            out.println("aMatcher.requireEnd() : " + aMatcher.requireEnd());
            // aMatcher.requireEnd(): True if the regex end requires anchoring bounds
            // aMatcher.requireEnd() : false

        }

    }

    public static void main(String[] args) {
        matcherInfo();

    }
}

Append methods

Matcher	appendReplacement(StringBuffer sb, String replacement)
StringBuffer	appendTail(StringBuffer sb)

These methods are used with matching methods like find or lookinAt . They both take a string buffer as a parameter . appendReplacement , will append the replacement of the match , to the string buffer. appenTail , will append the tail – the string after the found match – to the string buffer.

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.System.out;

class MatcherAppend {

    public static void matcherAppend() {

        String aString = "123;456;789";
        //  a string

        Pattern aPattern = Pattern.compile(";");
        //create a pattern

        Matcher aMatcher = aPattern.matcher(aString);
        //create a matcher from the pattern using aString

        StringBuffer stringBufferReplaced = new StringBuffer();
        while (aMatcher.find()) {
            // while the string contains ;

            aMatcher.appendReplacement(stringBufferReplaced, "-");
            // replace the ; with -
            // 123-456-
        }

        out.println(stringBufferReplaced);
        // 123-456-

        aMatcher.appendTail(stringBufferReplaced);
        // append the remaining string 789 to the string buffer

        out.println(stringBufferReplaced);
        // 123-456-789

    }

    public static void main(String[] args) {
        matcherAppend();
    }
}

Reset methods

Matcher	reset()
Matcher	reset(CharSequence input)

The reset methods affect the matching methods .

  • They will reset the start and end region
  • find will start matching from the start of the region

The reset methods doesn’t affect the anchoring bounds and the transparent bounds . reset(CharSequence input) will also replace the string against which we are doing matching .

import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.regex.MatchResult;
import static java.lang.System.out;

class PatternMatcherFind {

    public static void patternReset() {
        Pattern aPattern; // declare a pattern
        Matcher aMatcher; // declare a matcher

        String aString = "a;3;1;7;0;";
        //create a string

        aPattern = Pattern.compile(";");
        // compiler the pattern

        aMatcher = aPattern.matcher(aString);
        // get a matcher out of this pattern with aString as a parameter

        aMatcher.region(2, aString.length());
        // set the match region from 2 till the end of the string

        aMatcher.useAnchoringBounds(false);
        // set anchoring bounds to false

        aMatcher.useTransparentBounds(true);
        // set transparent bound to true

        // find first two matches
        aMatcher.find(); // first match is ; after 3
        aMatcher.find(); // second match is ; after 1

        out.println("aMatcher.start() : " + aMatcher.start());
        // print the start position of the second match
        // output : aMatcher.start() : 5

        aMatcher.reset();
        // reset the start and end region from 0 till the end of the string
        // find will start matching from the start

        out.println("aMatcher.regionStart() : " + aMatcher.regionStart());
        // output : aMatcher.regionStart() : 0

        out.println("aMatcher.regionEnd() : " + aMatcher.regionEnd());
        // output : aMatcher.regionEnd() : 10

        aMatcher.find();// first match is ; after a

        out.println("aMatcher.hasAnchoringBounds() : " + aMatcher.hasAnchoringBounds());
        // output : aMatcher.hasAnchoringBounds() : false

        out.println("aMatcher.hasTransparentBounds() : " + aMatcher.hasTransparentBounds());
        // output: aMatcher.hasTransparentBounds() : true

        out.println("aMatcher.start() : " + aMatcher.start());
        // print the start position of the first match ; after a
        // output : aMatcher.start() : 1

        aMatcher.reset("b;");
        // reset the string to match , to b;
        // reset the start and end region to 0 - 2

        out.println("aMatcher.regionStart() : " + aMatcher.regionStart());
        // output : aMatcher.regionStart() : 0

        out.println("aMatcher.regionEnd() : " + aMatcher.regionEnd());
        // output : aMatcher.regionEnd() : 2

        aMatcher.find();// first match is ; after b

        out.println("aMatcher.hasAnchoringBounds() : " + aMatcher.hasAnchoringBounds());
        // output : aMatcher.hasAnchoringBounds() : false

        out.println("aMatcher.hasTransparentBounds() : " + aMatcher.hasTransparentBounds());
        // output: aMatcher.hasTransparentBounds() : true

        out.println("aMatcher.start() : " + aMatcher.start());
        // print the start postion of the first match ; after b
        // output : aMatcher.start() : 1

    }

    public static void main(String[] args) {

        patternReset();
    }

}

Replace methods

String	replaceAll(String replacement)
String	replaceFirst(String replacement)

we can replace the found matches by a replacement strings . We have two methods , the first one will replace all the matches , the second one will only replace the first match . These methods are not affected by the region(int start, int end) , method and they will replace the matches from the start till the end of the string .

import java.util.regex.Pattern;
import java.util.regex.Matcher;
import static java.lang.System.out;

class MatcherReplace {

    public static void matcherReplace() {

        Pattern aPattern = Pattern.compile(";");
        // create an instance of the Pattern class with the compiled regex ;

        Matcher aMatcher = aPattern.matcher("a;b;c;d");
        // create a matcher instance from aPattern with the string "a;b;c;d"

        aMatcher.region(2, aSring.length());
        // set the region for matching the string from 2 till the end of the string
        // this doesn't affect the replace methods

        out.println(aMatcher.replaceAll("_"));
        // replace all the ; in a;b;c;d with _
        // output : a_b_c_d

        out.println(aMatcher.replaceFirst("_"));
        // replace the first ; in a;b;c;d with _
        // output : a_b;c;d

    }

    public static void main(String[] args) {
        matcherReplace();

    }
}

Other methods

Pattern pattern()

Pattern	pattern()

we can use this method to return the pattern used to created this matcher .

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.System.out;

class MatcherPattern {

    public static void matcherPattern() {

        Pattern aPattern = Pattern.compile("\\d[a-z]");
        // create a pattern

        Matcher aMatcher = aPattern.matcher("1a");
        // create a matcher

        out.println("The pattern used to create the matcher : " + aMatcher.pattern());
        // aMatcher.pattern() : get the pattern used to create the matcher
        // output : The pattern used to create the matcher : \d[a-z]

    }

    public static void main(String[] args) {
        matcherPattern();
    }
}

Matcher usePattern(Pattern newPattern)

Matcher	usePattern(Pattern newPattern)

This method is used to change the Pattern used by the matcher .

  • it doesn’t change the start and end region used to match the string
  • it doesn’t affect anchoring and transparent bounds
  • it doesn’t affect the find method
  • the last found match information such as the groups or the position is lost or reset.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.System.out;

class MatcherUsePattern {

    public static void matcherUsePattern() {

        Pattern aPattern = Pattern.compile("-");
        // create a Pattern

        Matcher aMatcher = aPattern.matcher("a-b-c;d");
        // create a Matcher from the pattern using aStringOne

        aMatcher.region(2, "a-b-c;d".length());
        // set the match start region to 2 and the match end region to the length of
        // "a-b-c;d"

        aMatcher.find();
        // find a match inside "a-b-c;d"

        out.println("found match at  : " + aMatcher.start());
        // aMatcher.start() : return the found match starting position
        // output : The found match start position is 3

        aMatcher.usePattern(Pattern.compile(";"));
        // change the pattern to ;
        // continue matching after the last found match
        // does not affect anchoring and transparent bounds
        // does not affect the match region
        // information about the last match is lost

        aMatcher.find();
        // continue finding a match inside "a-b-c;d"

        out.println("found match at  : " + aMatcher.start());
        // aMatcher.start() : return the found match starting position
        // output : found match at : 5

    }

    public static void main(String[] args) {
        matcherUsePattern();

    }

}

static String quoteReplacement(String s)

static String quoteReplacement(String s)

when replacing a string using the

  • replaceAll
  • replaceFirst

methods , we cannot use a string which contains the backslash or the dollar character as this will throw an exception . We can use this method to escape these two characters , and as such we can use the escaped string with the replace or append methods .

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.System.out;

class MatcherQuoteReplacement {

    public static void matcherQuoteReplacement() {

        Pattern aPattern = Pattern.compile("([a-z])(;)");
        // ([a-z]) : first capture group , match characters from a - z
        // (;) : second capture group , match ;

        Matcher aMatcher = aPattern.matcher("a;b;c;");

        try {
            aMatcher.replaceAll("\\");
            // in a string we cannot use \ directly , we must escape it by using a backslash
            // \\
            // for replaceAll \\ is used to escape a character
            // so it expect an escape character to follow \\
            // so it will throw an error

        } catch (Exception exception) {
            out.println("The exception is : " + exception.getMessage());
            // output : The exception is : character to be escaped is missing

            out.println(aMatcher.replaceAll("\\$"));
            // output $$$

            out.println(aMatcher.replaceAll("\\\\"));
            // output \\\

        }

        try {
            aMatcher.replaceAll("$");
            // $ is used to refer a group
            // we didn't use it to refer to any group
            // this will throw an exception

        } catch (Exception exception) {
            out.println("The exception is : " + exception.getMessage());
            // output : The exception is : Illegal group reference: group index is missing

            out.println(aMatcher.replaceAll("$1"));
            // replace the captured expression , with the first capture group
            // output : abc
        }

        // quoteReplacement can be used to escape \ and to escape $ so that they can be
        // used
        // in replaceAll replaceFirst and appendReplacement methods
        aMatcher.reset();
        String replaceWithABackSlash = Matcher.quoteReplacement("\\");
        StringBuffer stringBufferReplacement = new StringBuffer();
        while (aMatcher.find()) {
            aMatcher.appendReplacement(stringBufferReplacement, replaceWithABackSlash);
        }
        out.println(stringBufferReplacement);
        // output : \\\

    }

    public static void main(String[] args) {
        matcherQuoteReplacement();

    }
}