-5
什么是对正则表达式表达:正则表达式表达式
- 具有一定的字符串开头?如:以“/ retour,merci”开头
- 以某字符串结束?如:以“/blog-accueils.html”结尾
- 并包含字符串?如:包含 “数据/”
什么是对正则表达式表达:正则表达式表达式
正则表达式:
^ #charracter for begin string
$ #charracter for ending string
[a-zA-Z]+ #regex for a word or you can use \w+ which mean words
这样:
^[a-zA-Z]+$
如果你想匹配像/
字符添加它们这样\/
阅读:
http://www.regular-expressions.info/shorthand.html
测试用:
这里是你如何进行:
String patternStr = String.format(
"^%s.*%s.*%s$",
Pattern.quote("/retour,merci"),
Pattern.quote("data/"),
Pattern.quote("/blog-accueils.html")
);
Pattern pattern = Pattern.compile(patternStr);
System.out.println(
pattern.matcher("/retour,merci/foo/data/bar/blog-accueils.html").matches()
);
System.out.println(
pattern.matcher("/retour,merci/foo/ata/bar/blog-accueils.html").matches()
);
System.out.println(
pattern.matcher("/retour,merc/foo/data/bar/blog-accueils.html").matches()
);
输出:
true
false
false
尝试新鲜事物? – anubhava
你刚才描述了它。 – Mena
你在回答中发布的内容应该是你的问题。 – RanchiRhino