2013-06-18 168 views
2

我正在開發一個通用的應用程序,我需要匹配一個我無法解決的模式。正則表達式匹配模式

輸入字符串可能是:

12_B 99-23_9_23 

正如你所看到的例子,我的問題是,當我想與此模式匹配「B 99-23」。 分隔符可以是任何東西,不僅下劃線(前12 |乙99-23 | 9 | 23。)

現在,這是我...

Regex r = new Regex("^(?< pol>\\w+)_(?< fac>\\w+)_(?< end>\\w+)_(?< op>\\w+)"); 

我必須改變這一部分: (?< fac>\\w+) 該模式必須將所有內容都帶到下一個分隔符('_'),包括空格,數字,字符。 然後,我也會有這樣的結果:

pol = 12 
fac = B 99-23 
end = 9 
op = 23 

回答

2

嘗試使用這種模式:

^(?< pol>\w+)_(?< fac>[^_]+)_(?< end>\w+)_(?< op>\w+) 

[^_]有點character class這意味着「除下劃線匹配任何內容」。如果分隔符是|,則必須在您的模式中使用\|,因爲|在正則表達式中有特殊含義(儘管您不需要在字符類中轉義它)。就像這樣:

^(?< pol>\w+)\|(?< fac>[^|]+)\|(?< end>\w+)\|(?< op>\w+) 

在一個側面說明,我覺得它更容易使用正則表達式模式時要使用逐字字符串,因爲你將不必在打字這麼多的轉義序列:

new Regex(@"^(?< pol>\w+)\|(?< fac>[^|]+)\|(?< end>\w+)\|(?< op>\w+)"); 

然而,在這種情況下,你可能會更好只使用Split

var result = input.Split(new char[] { '_' }, 4); 
// result[0] = 12 
// result[1] = B 99-23 
// result[2] = 9 
// result[3] = 23 
+0

謝謝! 但是,這不適用於這個例子... 12 | B 99-23 | 9 | 23 它必須匹配,直到下一個分隔符。在上面的例子將是這樣的:^ 很抱歉,如果我不解釋它很好 –

+0

我已經(< pol> \ w +?)|(< fac>?)| |(< end> \ w +?)(< op> \ w +?)更新了我的答案,以包含適用於'|'字符的模式。你說過,「[分隔符可以是任何東西]」,但這沒有多大意義。你期望什麼分隔符?你需要處理混合分隔符嗎?應如何匹配「12 | B 99_23 | 9 | 23」? –

0

說明

問題的一部分是,\w還包括所有字母a-z,所有數字0-9和底部_。因此,如果您輸入文字可以使用_分隔符則表達式匹配\w+會感到困惑

因爲你有一個條件,允許下劃線作爲分隔符,我建議,而不是使用短手\w是你,而不是定義字符類,你」 d喜歡所需的文本和分隔符。

  • [0-9a-zA-Z]+將任何順序
  • [^a-zA-Z0-9]這是一個否定的字符類,並且將匹配這是不alphebetical任何字符或數字

此正則表達式匹配匹配一個或多個alphebetical或數字所有的價值,並將允許範圍廣泛的分隔符。

^(?<pol>[0-9a-zA-Z]+)[^a-zA-Z0-9](?<fac>[0-9a-zA-Z]+\s[0-9a-zA-Z]+-[0-9a-zA-Z]+)[^a-zA-Z0-9](?<end>[0-9a-zA-Z]+)[^a-zA-Z0-9](?<op>[0-9a-zA-Z]+)

enter image description here

要匹配fac組我假設領域將在以下格式:字母數字空間連字符數。

  • 組0將獲得整個匹配的字符串
  • 的命名組將被創建,但是在圖像1 = POL,2 = FAC,3 =端,和4 = OP。對不起,繪圖軟件無法處理命名的捕獲組。

C#代碼示例:

輸入文本

12_B 99-23_9_23 
11_a 11-11_1_11 
22|b 22-22|2|22 
33-c 33-33-3-33 
44,d 44-44,4,44 

代碼

using System; 
using System.Text.RegularExpressions; 
namespace myapp 
{ 
    class Class1 
    { 
     static void Main(string[] args) 
     { 
      String sourcestring = "source string to match with pattern"; 
      Regex re = new Regex(@"^(?<pol>[0-9a-zA-Z]+)[^a-zA-Z0-9](?<fac>[0-9a-zA-Z]+\s[0-9a-zA-Z]+-[0-9a-zA-Z]+)[^a-zA-Z0-9](?<end>[0-9a-zA-Z]+)[^a-zA-Z0-9](?<op>[0-9a-zA-Z]+)",RegexOptions.IgnoreCase | RegexOptions.Multiline); 
      MatchCollection mc = re.Matches(sourcestring); 
      int mIdx=0; 
      foreach (Match m in mc) 
      { 
      for (int gIdx = 0; gIdx < m.Groups.Count; gIdx++) 
       { 
       Console.WriteLine("[{0}][{1}] = {2}", mIdx, re.GetGroupNames()[gIdx], m.Groups[gIdx].Value); 
       } 
      mIdx++; 
      } 
     } 
    } 
} 

匹配

$matches Array: 
(
    [0] => Array 
     (
      [0] => 12_B 99-23_9_23 
      [1] => 11_a 11-11_1_11 
      [2] => 22|b 22-22|2|22 
      [3] => 33-c 33-33-3-33 
      [4] => 44,d 44-44,4,44 
     ) 

    [pol] => Array 
     (
      [0] => 12 
      [1] => 11 
      [2] => 22 
      [3] => 33 
      [4] => 44 
     ) 


    [fac] => Array 
     (
      [0] => B 99-23 
      [1] => a 11-11 
      [2] => b 22-22 
      [3] => c 33-33 
      [4] => d 44-44 
     ) 


    [end] => Array 
     (
      [0] => 9 
      [1] => 1 
      [2] => 2 
      [3] => 3 
      [4] => 4 
     ) 


    [op] => Array 
     (
      [0] => 23 
      [1] => 11 
      [2] => 22 
      [3] => 33 
      [4] => 44 
     ) 


)