[英]java Regular expressions about line contains multiple characters
我是Java的新手,我想就Java正则表达式提出一个问题。
如何检查行是否仅包含特定字符串,后接任何运算符。 另外,字符串的前一个不包含“ //”。
例如,如果line是:
//x; -> does not matches the criteria
x++; ->matches
x--; ->matches
x=1; ->matches
(x,y) ->matches
(x1,y) ->does not matches because we want only x not x1
x = 1 ; ->matches
提前致谢。
您可以使用以下基于负前瞻的正则表达式:
^(?:(?!//).)*\bx\b.*
在Java中使用:
boolean valid = str.matches("^(?:(?!//).)*\\bx\\b");
正则表达式分解:
^ # line start
(?:(?!//).)* # negative lookahead, match any char that doesn't have // at next position
\b # word boundary
x # literal x
\b # word boundary
.* # match every thing till end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.