Кода -копия выглядит следующим образом:
пакет com.hongyuan.test;
импортировать java.util.regex.matcher;
Импорт java.util.regex.pattern;
открытый класс regextest {
public static void main (string [] args) {
String str = "<html> <Head> <Title> тест regex </title> </head> <body> <p> Это простой тест regex </p> </body> </html>";
// разделить строку
String [] splitstr = pattern.compile ("[</? |>]"). Split (str);
для (int i = 0; i <splitstr.length; i ++) {
System.out.print (splitstr [i]+"");
}
System.out.println ();
// Судите, соответствует ли строка схеме формулы
boolean ismatching = pattern.compile ("^<(// w*)>.*</// 1> $"). Matcher (str) .matches ();
System.out.println (iSmatching);
// заменить строку
String repstr = pattern.compile ("<(/?) P>"). Matcher (str) .replaceall ("<$ 1H1>");
System.out.println (repstr);
// Извлечение строки
Matcher m = pattern.compile ("<Title> (.*) </Title>"). Matcher (str);
while (m.find ()) {
System.out.println (M.Group (1));
}
}
}