How to use regex in Java
The String class provides search methods which accept regular expression and search for it. The following program shows how to use regular expressions in Java program with strings:
package com.example;
/** This class is a Java tutorial for using regular expression in Java
*
* @author Extreme Java
*/
public class Test{
/** This method shows how to use regex in Java
*
* @param args
*/
public static void main(String[] args) {
String str1 = "This is a string";
System.out.println(str1.replaceAll("Th*", "+"));
}
}
output:
+is is a string
The output of the above program shows how the use of regular expression for replacing a part of the string.
No comments:
Post a Comment