Fiona
0
Q:

java regex

// Java program to demonstrate 
// Pattern.compile() method 
  
import java.util.regex.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
        // create a REGEX String 
        String REGEX = ".*www.*"; 
  
        // creare the string 
        // in which you want to search 
        String actualString 
            = "www.geeksforgeeks.org"; 
  
        // compile the regex to create pattern 
        // using compile() method 
        Pattern pattern = Pattern.compile(REGEX); 
  
        // get a matcher object from pattern 
        Matcher matcher = pattern.matcher(actualString); 
  
        // check whether Regex string is 
        // found in actualString or not 
        boolean matches = matcher.matches(); 
  
        System.out.println("actualString "
                           + "contains REGEX = "
                           + matches); 
    } 
} 
1
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);  
}}  
0
// Removes whitespace between a word character and . or ,
String pattern = "(\\w)(\\s+)([\\.,])";
System.out.println(EXAMPLE_TEST.replaceAll(pattern, "$1$3"));
-1
// Extract the text between the two title elements
pattern = "(?i)(<title.*?>)(.+?)()";
String updated = EXAMPLE_TEST.replaceAll(pattern, "$2");
-1

New to Communities?

Join the community