How to Work with Back References

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - How to Work with Back References

This Java example program shows how to work with Back References.

[edit] Java Source Code

  • Package: example.matcher
  • File: ReplaceExample.java
package example.matcher;
 
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class ReplaceExample {
    public static void main(String args[]) {
	String regex = "(\\w)(\\d)(\\w+)";
	Pattern pattern = Pattern.compile(regex);
 
	String candidate = "X99SuperJava";
	Matcher matcher = pattern.matcher(candidate);
	String tmp = matcher.replaceAll("$33");
 
	System.out.println("REPLACEMENT: " + tmp);
	System.out.println("ORIGINAL: " + candidate);
    }
}

[edit] What Result You Can Get

Run the program, you will get:

REPLACEMENT: 9SuperJava3
ORIGINAL: X99SuperJava

[edit] Required External Libraries and/or Files for this Java Example

Need nothing.


[edit] How to Run this Java Example Program

We recommend running this Java example program with Eclipse.

For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.

It's fairly easy.



[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools