[英]regex to get word between two undescores
我有一行像bla_bla**_**test**_1**023
並想提取_
和任何下划線之間的單詞,后跟數字_digit
,這是上面示例中的test
。
我嘗試了以下正則表達式,但不幸的是不起作用: [^_ ]+(?=[ _\\d])
- 它讓我得到“_digit”之前的所有單詞,而不僅僅是_digit
之前的_digit
這應該對你有用。 使用Pattern
和Matcher
進行環視。
public static void main(String[] args) {
String word= "bla_bla_test_1023";
Pattern p = Pattern.compile("(?<=_)([^_]+)(?=_\\d+)");
Matcher m = p.matcher(word);
while (m.find()) {
System.out.println(m.group());
}
}
開/關:
測試
[^_]*(?=_\d)
您想匹配除 _ 以外的所有內容,直到找到 _ 后跟一個數字。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.