简体   繁体   English

C#中的特定正则表达式

[英]Specific regex in C#

Can someone enlighten me on why the folloewing regex does not work in C#? 有人可以启发我为什么在C#中无法使用正则表达式吗? Thank you. 谢谢。

Regex 101 正则表达式101

Im trying to get raudi06 out of the following string (as explained in link): "\\n http://dsde.innogamescdn.com/8.30.1/23852/graphic/welcome/player_points.png?3ba0c\\">\\n raudi06\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t" 我试图从以下字符串中获取raudi06(如链接中所述):“ \\ n http://dsde.innogamescdn.com/8.30.1/23852/graphic/welcome/player_points.png?3ba0c\\">\\n raudi06 \\ t \\ t \\ t \\ t \\ t \\ t \\ t \\ t \\ t \\ t \\ t“

Here comes the code: 代码如下:

        Match match = new Regex(@"[^>]+>\\n\s+(.*?)\\").Match(playerText);
        return match.Groups[1].Value;

The value is an empty string, as opposed to the regex explanation on regex101.com. 该值是一个空字符串,与regex101.com上的regex说明相反。 Is this due to C# regex flavor? 这是由于C#正则表达式的味道吗? I don't know much about regex and hope that maybe you know about these specific details. 我对正则表达式了解不多,希望您也许对这些具体细节有所了解。

Thank you in advance! 先感谢您!

In fact the whitespace characters (\\n, \\t) were not literal. 实际上,空格字符(\\ n,\\ t)不是文字。 I found out about that by right clicking the variable in the watch window of Visual Studio and selecting the value option, which displayed the string without the literal whitespace characters. 我通过在Visual Studio的监视窗口中右键单击变量并选择value选项(通过显示不带文字空白字符的字符串)发现了这一点。

[^>]+>\s+([^\s]*)

Does work to match the string. 可以匹配字符串。 Thank you for your hint, Avinash Raj. 谢谢您的提示,Avinash Raj。

尝试以下一项:

System.Text.RegularExpressions.Regex.Match(input, "[^>]+>\s+(.*?)\s", System.Text.RegularExpressions.RegexOptions.Multiline)

When you see the \\n and the \\t in your string, they are just single caracter placeholders for newline and tab. 当您在字符串中看到\\ n和\\ t时,它们只是换行符和制表符的单个字符占位符。 This means that you will never find \\n by looking for a \\ and a n. 这意味着您永远不会通过查找\\和n来找到\\ n。

The pattern you're using will look specifically for a backslash followed by the literal n. 您正在使用的模式将专门查找反斜杠,后跟文字n。 To change this, simply remove one of the backslashes, leaving \\n instead of \\n 要更改此设置,只需删除一个反斜杠,使\\ n代替\\ n

The next challenge is the end criteria for your string. 下一个挑战是字符串的结束条件。 In your pattern you're looking for a backslash again (\\), but there is none, just tabs (\\t). 在您的模式中,您需要再次寻找一个反斜杠(\\),但没有一个,只是制表符(\\ t)。 This one you could change to \\t and it should work. 您可以将其更改为\\ t,它应该可以工作。

Match match = new Regex(@"[^>]+>\n\s+(.*?)\t").Match(playerText);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM