簡體   English   中英

關於行的Java正則表達式包含多個字符

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM