Java/Regular Expressions/Group — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 06:00, 1 июня 2010

Capturing Text in a Group in a Regular Expression

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
  public static void main(String[] argv) throws Exception {
    CharSequence inputStr = "abbabcd";
    String patternStr = "(a(b*))+(c*)";
    // Compile and use regular expression
    Pattern pattern = Pattern.rupile(patternStr);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.find();
    if (matchFound) {
      // Get all groups for this match
      for (int i = 0; i <= matcher.groupCount(); i++) {
        String groupStr = matcher.group(i);
      }
    }
  }
}





Find group number 1 of the second find

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
  public static void main(String[] argv) throws Exception {
    Pattern p = Pattern.rupile("t(est)");
    String candidateString = "This is a test. This is a test.";
    Matcher matcher = p.matcher(candidateString);
    matcher.find();
    String group_0 = matcher.group(0);
    String group_1 = matcher.group(1);
    System.out.println("Group 0 " + group_0);
    System.out.println("Group 1 " + group_1);
  }
}





find the starting point of the first subgroup

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
  public static void main(String args[]) {
    Pattern p = Pattern.rupile("t(est)");
    String candidateString = "This is a test. This is another test.";
    Matcher matcher = p.matcher(candidateString);
    int nextIndex = matcher.start(1);
    System.out.println(candidateString);
    System.out.println(nextIndex);
  }
}





Find the starting point of the second subgroup

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
  public static void main(String args[]) {
    Pattern p = Pattern.rupile("t(est)");
    String candidateString = "This is a test. This is another test.";
    Matcher matcher = p.matcher(candidateString);
    matcher.find();
    int startIndex = matcher.start(0);
    System.out.println(candidateString);
    System.out.println(startIndex);
  }
}





Getting the Indices of a Matching Group in a Regular Expression

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
  public static void main(String[] argv) throws Exception {
    CharSequence inputStr = "abbabcd";
    String patternStr = "(a(b*))+(c*)";
    Pattern pattern = Pattern.rupile(patternStr);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.find();
    if (matchFound) {
      // Get all groups for this match
      for (int i = 0; i <= matcher.groupCount(); i++) {
        // Get the group"s captured text
        String groupStr = matcher.group(i);
        // Get the group"s indices
        int groupStart = matcher.start(i);
        int groupEnd = matcher.end(i);
        // groupStr is equivalent to
        inputStr.subSequence(groupStart, groupEnd);
      }
    }
  }
}





Matcher.group(int) Method Example

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
  public static void main(String args[]) {
    Pattern p = Pattern.rupile("t(est)");
    String candidateString = "This is a test. This is another test.";
    Matcher matcher = p.matcher(candidateString);
    // Find group number 0 of the first find
    matcher.find();
    String group_0 = matcher.group(0);
    String group_1 = matcher.group(1);
    System.out.println("Group 0 " + group_0);
    System.out.println("Group 1 " + group_1);
    System.out.println(candidateString);
  }
}





Using a Non-Capturing Group in a Regular Expression

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
  public static void main(String[] argv) throws Exception {
    String inputStr = "abbabcd";
    String patternStr = "(a(?:b*))+(c*)";
    // (?:b*) is a non-capturing group
    Pattern pattern = Pattern.rupile(patternStr);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.find();
    if (matchFound) {
      for (int i = 0; i <= matcher.groupCount(); i++) {
        String groupStr = matcher.group(i);
      }
    }
  }
}





Using appendReplacement with Subgroup Replacements

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
  public static void main(String args[]) {
    Pattern p = Pattern.rupile("(another) (test)");
    StringBuffer sb = new StringBuffer();
    String candidateString = "This is another test.";
    String replacement = "$1 AAA $2";
    Matcher matcher = p.matcher(candidateString);
    matcher.find();
    matcher.appendReplacement(sb, replacement);
    String msg = sb.toString();
    System.out.println(msg);
  }
}





Using the Captured Text of a Group within a Pattern

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
  public static void main(String[] argv) throws Exception {
    String patternStr = "<(\\S+?).*?>(.*?)</\\1>";
    Pattern pattern = Pattern.rupile(patternStr);
    Matcher matcher = pattern.matcher("");
    // Set the input
    matcher.reset("xx <tag a=b> yy </tag> zz");
    // Get tagname and contents of tag
    boolean matchFound = matcher.find(); 
    String tagname = matcher.group(1); 
    String contents = matcher.group(2); 
    matcher.reset("xx <tag> yy </tag0>");
    matchFound = matcher.find(); 
  }
}





Using the Captured Text of a Group within a Replacement Pattern

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
  public static void main(String[] argv) throws Exception {
    String patternStr = "\\((\\w+)\\)";
    String replaceStr = "<$1>";
    Pattern pattern = Pattern.rupile(patternStr);
    CharSequence inputStr = "a (b c) d (ef) g";
    Matcher matcher = pattern.matcher(inputStr);
    String output = matcher.replaceAll(replaceStr);
  }
}





Working with Groups: characters and digits

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
  public static void main(String args[]) {
    Pattern p = Pattern.rupile("\\w\\d");
    String candidate = "AAA C1 ";
    Matcher matcher = p.matcher(candidate);
    if (matcher.find()) {
      String tmp = matcher.group(0);
      System.out.println(tmp);
    }
  }
}





Working with Subgroups

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
  public static void main(String args[]) {
    Pattern p = Pattern.rupile("\\w(\\d)");
    String candidate = "w5 ";
    Matcher matcher = p.matcher(candidate);
    if (matcher.find()) {
      String tmp = matcher.group(0);
      System.out.println(tmp); 
      tmp = matcher.group(1); 
      System.out.println(tmp);
    }
  }
}