Java by API/java.util.regex/Pattern

Материал из Java эксперт
Версия от 14:47, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Pattern.CANON_EQ

/*
égal matches input égal
égal matches input e?gal
égal does not match input e?gal
égal does not match input e"gal
égal does not match input e�gal
 * */
import java.util.regex.Pattern;
/**
 * CanonEqDemo - show use of Pattern.CANON_EQ, by comparing varous ways of
 * entering the Spanish word for "equal" and see if they are considered equal by
 * the RE-matching engine.
 * 
 * @version $Id: CanonEqDemo.java,v 1.3 2004/03/21 20:06:20 ian Exp $
 */
public class MainClass {
  public static void main(String[] args) {
    String pattStr = "\u00e9gal"; // �gal
    String[] input = { "\u00e9gal", // �gal - this one had better match :-)
        "e\u0301gal", // e + "Combining acute accent"
        "e\u02cagal", // e + "modifier letter acute accent"
        "e"gal", // e + single quote
        "e\u00b4gal", // e + Latin-1 "acute"
    };
    Pattern pattern = Pattern.rupile(pattStr, Pattern.CANON_EQ);
    for (int i = 0; i < input.length; i++) {
      if (pattern.matcher(input[i]).matches()) {
        System.out.println(pattStr + " matches input " + input[i]);
      } else {
        System.out.println(pattStr + " does not match input " + input[i]);
      }
    }
  }
}





Pattern.CASE_INSENSITIVE

/*
IGNORE_CASE match true
MATCH_NORMAL match was false
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainClass {
  public static void main(String[] argv) {
    String pattern = "^q[^u]\\d+\\.";
    String input = "QA777. is the next flight.";
    Pattern reCaseInsens = Pattern.rupile(pattern, Pattern.CASE_INSENSITIVE);
    Pattern reCaseSens = Pattern.rupile(pattern);
    boolean found;
    Matcher m;
    m = reCaseInsens.matcher(input); // will match any case
    found = m.lookingAt();           // will match any case
    System.out.println("IGNORE_CASE match " + found);
    m = reCaseSens.matcher(input); // Get matcher w/o case-insens flag
    found = m.lookingAt();         // will match case-sensitively
    System.out.println("MATCH_NORMAL match was " + found);
  }
}





Pattern: compile("[a-z]+")

/*
 * Output:
Match: www
Match: java
Match: s
Match: com
 * */
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainClass {
  public static void main(String args[]) {
    // Match lowercase words.
    Pattern pat = Pattern.rupile("[a-z]+");
    Matcher mat = pat.matcher("www.jexp.ru.");
    while (mat.find())
      System.out.println("Match: " + mat.group());
  }
}





Pattern: compile("d.*ian")

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainClass {
  public static void main(String[] args) {
    Pattern myRE = Pattern.rupile("d.*ian");
    Matcher matcher = myRE.matcher("danian devonian dan");
    matcher.lookingAt();
    String result = matcher.group(0);
    System.out.println(result);
  }
}





Pattern: compile("e.+?d")

/*
 * Output:
Match: extend
Match: end
 * */
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainClass {
  public static void main(String args[]) {
    Pattern pat = Pattern.rupile("e.+?d");
    Matcher mat = pat.matcher("extend cup end table");
    while (mat.find())
      System.out.println("Match: " + mat.group());
  }
}





Pattern: compile(String text)

/*
 * Output:
Matches
No Match
 
 * */
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainClass {
  public static void main(String args[]) {
    Pattern pat;
    Matcher mat;
    boolean found;
    pat = Pattern.rupile("Java");
    mat = pat.matcher("Java");
    found = mat.matches();
    if (found)
      System.out.println("Matches");
    else
      System.out.println("No Match");
    System.out.println();
    mat = pat.matcher("Java 2");
    found = mat.matches();
    if (found)
      System.out.println("Matches");
    else
      System.out.println("No Match");
  }
}





Pattern: compile("(\\w+)\\s(\\d+)")

import java.util.regex.*;
public class MainClass {
  public static void main(String[] args) {
    Pattern patt = Pattern.rupile("(\\w+)\\s(\\d+)"); 
    Matcher matcher = patt.matcher("Bananas 123"); 
    matcher.lookingAt();
    System.out.println("Name: " + matcher.group(1)); 
    System.out.println("Number: " + matcher.group(2)); 
  }
}





Pattern: matcher(String text)

/*
 * Output:
Matches
No Match
 
 * */
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainClass {
  public static void main(String args[]) {
    Pattern pat;
    Matcher mat;
    boolean found;
    pat = Pattern.rupile("Java");
    mat = pat.matcher("Java");
    found = mat.matches();
    if (found)
      System.out.println("Matches");
    else
      System.out.println("No Match");
    System.out.println();
    mat = pat.matcher("Java 2");
    found = mat.matches();
    if (found)
      System.out.println("Matches");
    else
      System.out.println("No Match");
  }
}





Pattern: split(String str)

/*
 * Output:
Next token: www
Next token: jexp
Next token: com
Next token: java
Next token: javascript
Next token: API
Next token: example
 * */
import java.util.regex.Pattern;
public class MainClass {
  public static void main(String args[]) {
    Pattern pat = Pattern.rupile("[ ,.!]");
    String strs[] = pat.split("www.jexp.ru java javascript API example.");
    for (int i = 0; i < strs.length; i++)
      System.out.println("Next token: " + strs[i]);
  }
}