[英]String expression not matched string wide by java regex
我正在嘗試匹配以下字符串:
÷7%3@x#2$+÷5%3@x#2$-4
符合以下條件:
string = string.replaceAll("÷(.+)%(.+)@x#([0-9]+)\\$","÷$1x#$3\\$%$2@");
從上面的調用中,我看到: $1 = (.+) and $2 = (.+) and $3 = ([0-9]+)
在調用replaceAll之后:字符串已更改為: ÷7%3@x#4$+÷5x#2$%3@-4
請注意 ,正則表達式僅應用於÷5%3@x#2$
而不應用於÷7%3@x#2$
我需要正則表達式來匹配整個表達式。
正則表達式可能有什么問題?
我正在發布此答案,這是關於要完成的工作的有效方法。 不是解決問題的最優雅的方法,但是它可以工作。
在我的回答中,我使用Pattern和Matcher類對每次出現的正則表達式進行范圍划分,然后將它們替換為Pattern和Matcher找到它們中的每一個。
Pattern pattern = Pattern.compile("÷.+?%.+?@x#\\d+\\$");
Matcher matchk = patternk.matcher(string);
while(matchk.find()){
String string1 = matchk.group();
String string2 = string1;
string2 = string2.replaceAll("÷(.+)%(.+)@x#(\\d+)\\$","÷$1x#$3\\$%$2@");
string = string.replace(string1,string2);}
結果是我想要獲取的格式化字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.