what is regex in java code example
Example 1: java regex
import java.util.regex.*;
public class RegexExample1{
public static void main(String args[]){
//1st way
Pattern p = Pattern.compile(".s");//. represents single character
Matcher m = p.matcher("as");
boolean b = m.matches();
//2nd way
boolean b2=Pattern.compile(".s").matcher("as").matches();
//3rd way
boolean b3 = Pattern.matches(".s", "as");
System.out.println(b+" "+b2+" "+b3);
}}
Example 2: regex java
import java.io.*;
import java.util.regex.*;
public class testRegex {
private static Pattern pattern;
private static Matcher matcher;
public static void main(String args[]) {
pattern = Pattern.compile("Hugo");
matcher = pattern.matcher("Hugo Etiévant");
while(matcher.find()) {
System.out.println("Trouvé !");
}
}
}
Example 3: java regex ip
public static boolean isIP(String ip) {
Pattern p = Pattern.compile("(([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\\.){3}([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])");
Matcher m = p.matcher(ip);
return m.matches();
}