亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

正則表達式雙運算符是單計數

正則表達式雙運算符是單計數

青春有我 2022-07-14 10:18:58
我想檢查是否有雙重運算符。例如 :整數結果 = x+y;結果operatorCounter = 2,它正在工作。但:for(;i<size;i++)結果operatorCounter = 3應該是operatorCounter = 2。我的正則表達式String doubleOperatorPattern = "\'.\\++\'";我想要的運算符: (++) (--) (==) (&&) (||)public void findOperator(String file){    String operatorPattern = "['+''-''*''/''=''<''>''<=''>=''&''|''^''!''\\-?']";    Pattern pattern = Pattern.compile(operatorPattern);    Matcher matcher = pattern.matcher(file);    while (matcher.find()) {        operatorCounter++;    }    String doubleOperatorPatternString = "['==''++''--''&&''||']";    Pattern doubleOperatorPattern =     Pattern.compile(doubleOperatorPatternString);    Matcher doubleOperatorMatcher = doubleOperatorPattern.matcher(file);    while(doubleOperatorMatcher.find()){        operatorCounter--;    }}
查看完整描述

1 回答

?
料青山看我應如是

TA貢獻1772條經驗 獲得超8個贊

您可以在單字符運算符,和之前定義 the++和其他兩個字符運算符,如+=or -=first 。如果我們遵循Operators 文檔并添加所有 Java 運算符,那么正則表達式會因為轉義而變得討厭:+-=


Pattern pattern = Pattern.compile(

        "\\+\\+|--|" +          // ++ --

        "\\+=|-=|\\*=|" +       // += -= *=

        "/=|%=|&=|\\^=|" +      // /= %= &= ^=

        "\\|=|<<=|>>>=|>>=|" +  // |= <<= >>>= >>=

        "<<|>>>|>>|" +          // << >>> >>

        "==|!=|<=|>=|" +        // == != <= >=

        "&&|\\|\\||" +          // && ||

        "\\+|-|~|!|" +          // + - ~ !

        "\\*|/|%|" +            // * / %

        "\\+|&|\\^|\\||" +      // + & ^ |

        "<|>|=|" +              // < > =

        "instanceof"            // instanceof

);


Matcher matcher = pattern.matcher("for(;i<size;i++)");

int count = 0;

while (matcher.find()) {

  count++;

}

System.out.println(count);

但它會找到<并++打印 2。


請注意,這個正則表達式仍然不支持三元運算符? :。


查看完整回答
反對 回復 2022-07-14
  • 1 回答
  • 0 關注
  • 94 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號