2

I ran into a problem with my regular expressions, I'm using regular expressions for obtaining data from the string below:

 "# DO NOT EDIT THIS MAIL BY HAND #\r\n\r\n[Feedback]:hallo\r\n\r\n# DO NOT EDIT THIS MAIL BY HAND #\r\n\r\n"

So far I got it working with:

String sFeedback = Regex.Match(Message, @"\[Feedback\]\:(?<string>.*?)\r\n\r\t\n# DO NOT EDIT THIS MAIL BY HAND #").Groups[1].Value;

This works except if the header is changed, therefore I want the regex to read from [feedback]: to the end of the string. (symbols, ascii, everything..)

I tried: \[Feedback]:(?<string>.*?)$

Above regular expression does work in some regular expression builders online but in my c# code its not working and returns a empty string. What's wrong?

4

3 に答える 3

2

問題は、正規表現をコンパイルするときに .使用するか、次を使用してインライン化しない限り、改行と一致しないことです。RegexOptions.Singleline(?s)

(?s)\[Feedback\]:(.*)$
于 2012-09-25T09:45:52.880 に答える
0

エスケープ文字がありません。

また、C# コードでは名前でグループを参照していないため、正規表現をこれにさらに単純化できます。

\[Feedback\]:(.*)$
于 2012-09-25T09:45:01.383 に答える
0

$正規表現での意味:

一致は、文字列\nの末尾か、行または文字列の末尾の前で発生する必要があります。

そして.意味:

を除く任意の 1 文字に一致します\n

この単純な正規表現を使用してみてください:

\[Feedback\]:(?<string>.*)
于 2012-09-25T09:45:05.660 に答える