我需要匹配字符串中最後發生的大寫字母和另一個字符之間的所有字符。輸入文字:CLEVER狐狸跳在夜晚的大牆和(2洞)牆上。正則表達式匹配最後發生的大寫字母和字符串中的另一個字符之間的所有字符
正則表達式使用:
(?<=\b[A-Z]+\s)(.+?)(?=\sin)
上述正則表達式給出fox JUMPED OVER the big and (Hole 2) wall
預期輸出:the big and (Hole 2) wall
誰能破解這個?
我需要匹配字符串中最後發生的大寫字母和另一個字符之間的所有字符。輸入文字:CLEVER狐狸跳在夜晚的大牆和(2洞)牆上。正則表達式匹配最後發生的大寫字母和字符串中的另一個字符之間的所有字符
正則表達式使用:
(?<=\b[A-Z]+\s)(.+?)(?=\sin)
上述正則表達式給出fox JUMPED OVER the big and (Hole 2) wall
預期輸出:the big and (Hole 2) wall
誰能破解這個?
這可能不是最有效的解決方案,但它似乎工作:
String text = "The CLEVER fox JUMPED OVER the big wall in the night.";
String regex = "(\\b[A-Z]+\\s)(?!.*\\b[A-Z]+\\b)(.+?)(\\sin)";
Matcher m = Pattern.compile(regex).matcher(text);
if (m.find()) {
System.out.println(m.group(2));
}
它使用負前瞻,以確保沒有更多的文字大寫單詞捕捉通緝前數據。
如何:
[A-Z][\s.](?!.*?[A-Z])(.*)\sin
EXPL:找到一個大寫字母后跟一個空格,後面沒有任何後跟一個大寫字母。然後捕捉任何東西,但不包括給定單詞後面的空格。
這隻捕獲想要的部分。
問候
如何:
^.*(?:\b[A-Z]+\b)(.+?)(?=\sin)
說明:
The regular expression:
(?-imsx:^.*(?:\b[A-Z]+\b)(.+?)(?=\sin))
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with^and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
----------------------------------------------------------------------
[A-Z]+ any character of: 'A' to 'Z' (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
.+? any character except \n (1 or more times
(matching the least amount possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
in 'in'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
如果有什麼( 「中」)這個詞出現不止一次最後大寫單詞後? – lrn
它應該與上次發生後第一次出現的「in」相匹配大寫字母 – immzi