[英]How to write a regular expression in Java to match a text at the beginning followed by a special character?
我想用java編寫一個程序來檢查字符串是否以特定文本開頭,后跟“|”
例如
String a ="pro";
String b ="pro|100";
String c ="pro|loc";
String d ="pro|book|I'd";
String text ="pro";
String reg ="^"+text+[|]*; //this does not seem to work
正則表達式應該匹配上面的所有a
, b
, c
, d
看起來您可能正在尋找類似的東西
String reg ="^" + Pattern.quote(text) +"($|[|].*)";
你可以刪除.*
如果你想使用Matcher#find
或者在Matcher#matches
情況下保留它。
Pattern.quote
生成表示作為參數傳遞的文字的正則表達式。 例如,如果您的text
是"a+b"
它將返回等效於"a\\\\+b"
正則表達式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.