2016-05-16 57 views
2

Iam新增了java,我想提出一個關於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

這對於_parser_來說看起來更好。你的項目是關於什麼的? –

回答

0

您可以使用該負前瞻基於正則表達式:

^(?:(?!//).)*\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 

RegEx Demo

相關問題