3

私は私を困惑させているように見える単純なシナリオがあります。コメントアウトされていない2つのタグの間のテキストを取得したい。次に例を示します。

// Example of commented text
// :Start
// <I don't want to get this text>
// :End


:Start
<Here is the text i want>
:End

解決:

皆様のご協力に感謝いたします。答えを超高速で受け取り、私が必要としていることを正確に実行します。自分の状況に最も適しているため、次の正規表現を使用しました。Tim Pietzckerに特に感謝します:

(?sm)(?<=^:Start\s*)(?:(?!^:End).)*
4

3 に答える 3

2

これを試して:

(?sm)(?<=^:Start\s*)(?:(?!^:End).)*

説明:

(?sm)     # Set options: . matches newline, ^ matches start-of line
(?<=      # Assert that this regex can be matched before the current position:
 ^        #  Start of line
 :Start   #  :Start
 \s*      #  Any whitespace
)         # End of lookahead
(?:       # Try to match...
 (?!      # (unless the following regex could be matched here:)
   ^      #  Start of line
   :End   #  :End
 )        #  End of lookahead
 .        # ... any character
)*        # Repeat any number of times
于 2012-10-09T17:37:44.603 に答える
1

私はこれに行きますが、十分に堅牢であるようです。複数の行もキャッチします。

(?s)(?<=(?<!/+\s*):Start\s+)(?!//).+\s(?=:End)

(?s) を SingleLine オプションの開始時に使用します。

于 2012-10-09T18:07:58.133 に答える
0

このパターンはそれを行う必要があります。基本的に、タグは行の先頭にある必要があります。これにより、実際のタグとコメント付きのタグが区別されます。

"\n:Start\n([^\n\/]+)\n:End"

Python での例を次に示します。sあなたの例のテキストです。

r = re.search("\n:Start\n([^\n\/]+)\n:End", s)
r.group(1)
'<Here is the text i want>'

.NET の構文については完全にはわかりませんが、これを見ると、次のようになるはずです。

foreach (Match match in Regex.Matches(s, "\n:Start\n([^\n\/]+)\n:End"))
    Console.WriteLine("'{1}'), match.Groups[1].Value)
于 2012-10-09T17:26:55.763 に答える