-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