命名组(RegEx) [英] Named groups (RegEx)

查看:148
本文介绍了命名组(RegEx)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我正在尝试使用命名组来解析字符串.

一个例子是:
退出代码:0;会话ID为RDP-Tcp#2

我的尝试是:
((exitCode)+(\ s)*:(\ s)*(?< exitCode> [^;] +)(\ s)*;(\ s)*(会话ID为)(\ s) *(?< sessionID> [^;] *)(\ s)*'');

我在语法上哪里出错?
谢谢


Hey all,
I''m trying to use named group to parse a string.

An example is:
exitcode: 0; session id is RDP-Tcp#2

and my try is:
("(exitCode)+(\s)*:(\s)*(?<exitCode>[^;]+)(\s)*;(\s)*(session id is)(\s)*(?<sessionID>[^;]*)(\s)*");

Where do I syntacticaly wrong?
Thanks


推荐答案

请参阅此页面,了解命名组的使用;

http://regexadvice.com/blogs/dneimke/archive/2005/01/13/256.aspx [ ^ ]
See this page on the use of Named Groups;

http://regexadvice.com/blogs/dneimke/archive/2005/01/13/256.aspx[^]


是什么让您认为语法上有些错误?您收到错误消息了吗?您用来运行此正则表达式的C#代码是什么(例如,如果您忽略空格或忽略大小写,这会让我们知道)?另外,我不确定您要匹配哪种输入字符串,但我想至少在该第一个匹配条件下,您的正则表达式不正确.例如,这可能会显示为匹配项:
What makes you think there is something syntactically incorrect? Did you get an error message? What is the C# code you''re using to run this regular expression (that will let us know, for example, if you are ignoring whitespace or if you are ignoring case)? Also, I''m not sure what variety of input strings you are trying to match, but I imagine your regex is incorrect at least based on that first match condition. For example, this will probably show up as a match:
exitcodeexitcodeexitcode: 0; session id is RDP-Tcp#2


另外,我不确定您为什么要在正则表达式的末尾检查尾随分号,因为没有尾随分号(也许有时会出现?).


Also, I''m not sure why you check for a trailing semicolon at the end of your regex, as there is no trailing semicolon (perhaps there is sometimes?).


(\ s)*
下的一条曲线 和一条消息:
错误1无法识别的转义序列"

原因很简单:
(exitCode)+(\ s)*:(\ s)*(?< exitCode> [^;] +)(\ s)*;(\ s)*(会话ID为)(\ s)*( ?< sessionID> [^;] *)(\ s)*"
字符"\"被解释为C#字符串转义,并且试图告诉您它不知道转义序列"\ s".要解决此问题,请用"\\"替换"\"字符,或在开头的双引号前放置"@"字符:
@(exitCode)+(\ s)*:(\ s)*(?< exitCode> [^;] +)(\ s)*;(\ s)*(会话ID为)(\ s)* (?< sessionID> [^;] *)(\ s)*"

对于您的实际正则表达式,请尝试以下操作:
"I get a curved line under (\s)*
and a message:
Error 1 Unrecognized escape sequence"

The reason is simple:
"(exitCode)+(\s)*:(\s)*(?<exitCode>[^;]+)(\s)*;(\s)*(session id is)(\s)*(?<sessionID>[^;]*)(\s)*"
The ''\'' character is being interpretted as a C# string escape, and it is trying to tell you that it doesn''t know the escape sequence ''\s''. To fix, either replace the ''\'' characters with ''\\'', or put an ''@'' character before the opening double quote:
@"(exitCode)+(\s)*:(\s)*(?<exitCode>[^;]+)(\s)*;(\s)*(session id is)(\s)*(?<sessionID>[^;]*)(\s)*"

For your actual regex, try this:
public static Regex regex = new Regex(
      "(?:exitcode\\s*\\:\\s*)(?<ExitCode>\\d*)(?:\\s*;\\s*session\\sid\\s"+
      "is\\s)(?<SessionCode>.*)",
    RegexOptions.IgnoreCase
    | RegexOptions.CultureInvariant
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );



值得一本Expresso的副本
Expresso [ ^ ]它是免费的,确实有助于解决正则表达式!



It is well worth getting a copy of Expresso
Expresso[^] It''s free and it really helps work out regexes!


这篇关于命名组(RegEx)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆