2014-01-17 107 views
0

我需要匹配字符串中最後發生的大寫字母和另一個字符之間的所有字符。輸入文字:CLEVER狐狸跳在夜晚的大牆和(2洞)牆上。正則表達式匹配最後發生的大寫字母和字符串中的另一個字符之間的所有字符

正則表達式使用:

(?<=\b[A-Z]+\s)(.+?)(?=\sin)

上述正則表達式給出fox JUMPED OVER the big and (Hole 2) wall

預期輸出:the big and (Hole 2) wall

誰能破解這個?

+0

如果有什麼( 「中」)這個詞出現不止一次最後大寫單詞後? – lrn

+0

它應該與上次發生後第一次出現的「in」相匹配大寫字母 – immzi

回答

2

這可能不是最有效的解決方案,但它似乎工作:

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)); 
} 

它使用負前瞻,以確保沒有更多的文字大寫單詞捕捉通緝前數據。

+0

謝謝。這像一個魅力。不好意思,因爲我正在努力學習,但有沒有什麼辦法可以將第二個捕獲組作爲輸出,因爲這是我唯一期望的輸出。 – immzi

+0

我想我得到了我想要的。這裏是修改後的正則表達式給了我我需要的東西(?=(\ b [AZ] + \ s)(?!。* \ b [AZ] + \ b))(。+?)(?= \ sin ) – immzi

+0

@immzi:是的,我沒有意識到你想要那樣,但你說得對。 – Keppil

1

你可以簡單地排除大寫字符在你的第二個匹配表達

(?<=\b[A-Z]+\s)([^A-Z]+)(?=\sin)

這將迫使第一部分匹配The CLEVER fox JUMPED OVER,第二匹配表達式將產生the big wall,最後一個匹配的唯一in在你的測試句子中的順序。

+0

感謝您的解決方案。這工作。但是如果我在單個大寫字母之間添加一個單詞。[更新了問題]該正則表達式失敗。任何解決方案。非常感謝您的輸入 – immzi

+0

@immzi:我的解決方案也適用於調整後的文本。 – Keppil

+0

如果你可以有個別大寫字母,負面預測是最好的答案。 – rluta

1

如何:

[A-Z][\s.](?!.*?[A-Z])(.*)\sin 

EXPL:找到一個大寫字母后跟一個空格,後面沒有任何後跟一個大寫字母。然後捕捉任何東西,但不包括給定單詞後面的空格。

這隻捕獲想要的部分。

問候

0

如何:

^.*(?:\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 
---------------------------------------------------------------------- 
+0

我很抱歉,但我似乎沒有得到你給出的兩個正則表達式中的任何一個的結果。 – immzi

+0

@immzi:奇怪,它在這裏工作。你是否在第一組中獲得了結果? – Toto

+0

沒有結果 – immzi

相關問題