我是java的新手,我想提出一个关于java正则表达式的问题。
如何检查一行是否仅包含特定字符串,后跟任何运算符。此外,字符串的前一个不包含“//”。
例如,如果行是:
//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
提前致谢。
答案 0 :(得分:0)
你可以使用这种基于正则表达式的负前瞻:
^(?:(?!//).)*\bx\b.*
在Java中使用:
boolean valid = str.matches("^(?:(?!//).)*\\bx\\b");
RegEx分手:
^ # 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