RegEx匹配撇号

时间:2011-11-08 21:57:09

标签: regex

我有以下文件:http://pastebin.com/PZYSv1i3

此文件中的每一行都以作为分隔符的apotrophe结束。我需要一个与IMD + F ++ :::和分隔符(')之间的每个撇号匹配的RegEx。

我在下面的RegEx中匹配IMD + F ++ :::和以下分隔符之间的所有文本,但是我需要一个仅获取多余撇号的RegEx。

(?<=IMD\+F\+\+:::)(.*?)(?='\n)

1 个答案:

答案 0 :(得分:0)

.NET中的以下正则表达式:

(?<=IMD\+F\+\+:::.*)'(?!\r?\n)

只匹配你想要的“额外”撇号。

Regex regexObj = new Regex(@"(?<=IMD\+F\+\+:::.*)'(?!\r\n)");
    Match matchResults = regexObj.Match(subjectString);
    while (matchResults.Success) {
        // matched text: matchResults.Value
        // match start: matchResults.Index
        // match length: matchResults.Length
        matchResults = matchResults.NextMatch();
    } 

说明:

    "
(?<=      # Assert that the regex below can be matched, with the match ending at this position (positive lookbehind)
   IMD       # Match the characters “IMD” literally
   \+        # Match the character “+” literally
   F         # Match the character “F” literally
   \+        # Match the character “+” literally
   \+        # Match the character “+” literally
   :::       # Match the characters “:::” literally
   .         # Match any single character that is not a line break character
      *         # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)
'         # Match the character “'” literally
(?!       # Assert that it is impossible to match the regex below starting at this position (negative lookahead)
   \r        # Match a carriage return character
      ?         # Between zero and one times, as many times as possible, giving back as needed (greedy)
   \n        # Match a line feed character
)
"