正则表达式怎么用

来源:百度知道 编辑:UC知道 时间:2024/05/28 06:48:12
正则的教程看了一些,也看懂了一些正则表达式,但是在实际中不会用呀,比如^The,是检查字符串里是否以"The"开头,但着怎么用呢,要编写一个测试Test.java怎么写呢
public class Tes{
public static void main(String args[]){
String str="There";
这里写什么呢
}
}

import java.util.regex.*;

public class Tes{
public static void main(String args[]){
String str="There";

try {
boolean foundMatch = str.matches("^The");
System.out.println(foundMatch);
} catch (PatternSyntaxException ex) {
// Syntax error in the regular expression
}

}
}