การคัดลอกรหัสมีดังนี้:
แพ็คเกจ com.hongyuan.test;
นำเข้า java.util.regex.matcher;
นำเข้า java.util.regex.pattern;
ชั้นเรียนสาธารณะ regextest {
โมฆะคงที่สาธารณะหลัก (สตริง [] args) {
string str = "<html> <head> <title> การทดสอบ Regex </title> </head> <body> <p> นี่คือการทดสอบ Regex ง่าย ๆ </p> </body> </html>";
// แยกสตริง
String [] splitstr = pattern.compile ("[</? |>]"). แยก (str);
สำหรับ (int i = 0; i <splitstr.length; i ++) {
System.out.print (splitstr [i]+"");
-
System.out.println ();
// ตัดสินว่าสตริงตรงกับรูปแบบสูตร
บูลีน 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);
ในขณะที่ (m.find ()) {
System.out.println (M.Group (1));
-
-
-