![](/img/trans.png)
[英]Java regular expression to match the pattern for equals sign between two or more words that has an exclamatory as a delimeter between the ranges
[英]regular expression text between two sign
我有一個文本,我想用適當的值替換其中的變量,而我的變量位於兩個@之間。 當我使用[/(?m)@.*?@/]獲取這些文本時,它還會返回第一個和最后一個@之前和之后的文本。 我怎么只能在這兩個@符號之間獲取文本。 提前致謝。 我在Java中使用String.split(“”)方法。 例如,我想在以下字符串上使用:
這是@ t @ o做結果的最佳方法!
我想得到以下兩個結果:
最好的
Ť
在Java中,您可以使用此正則表達式在第一個@
第二個@
之間獲取值:
String repl = input.replaceFirst("(?m)^[^@]*@([^@]*)@.*$" "$1");
在第一個@
最后一個@
之間獲取值:
String repl = input.replaceFirst("(?m)^[^@]*@(.*?)@[^@]*$" "$1");
要查找多個匹配項,請使用Pattern
, Matcher
:
Pattern p = Pattern.compile("@([^@]*)@"):
Matcher m = p.matcher(p);
while (m.find()) {
System.out.prinln(m.group(1));
}
Split()
是錯誤的工具,請使用Matcher()
方法代替。
String s = "this is @the best@ possible way @t@o do result!!!";
Pattern p = Pattern.compile("@([^@]*)@");
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group(1));
}
產量
the best
t
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.