代码意图:寻找两个相连的运算符号(+-*/)
按照正规语法这样写,运行会报错:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class OneTimeAtLeast
{
public static void main(String[] args)
{
String str = "12*/34";
Matcher m = Pattern.compile("[\\+-\\*/]").matcher(str);
if (m.find())
System.out.println(m.start());
else
System.out.println("没有匹配到!");
}
}
可是用斜杠 / 而不是反斜杠 \ 来转意却能运行成功
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class OneTimeAtLeast
{
public static void main(String[] args)
{
String str = "12*/34";
Matcher m = Pattern.compile("[/+-/*/]").matcher(str);
if (m.find())
System.out.println(m.start());
else
System.out.println("没有匹配到!");
}
}
|
|