Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用正则表达式提取字符串的内容_C#_Regex - Fatal编程技术网

C# 使用正则表达式提取字符串的内容

C# 使用正则表达式提取字符串的内容,c#,regex,C#,Regex,我有像b___5这样的文本 结果输出应该是尖括号()之间的内容,即GetSupportURLs 我尝试以下常规经验,但没有运气 var result= Regex.Match("<GetSupportUrls>b__5", @"\<([^>]*)\)").Groups[1].Value; var result=Regex.Match(“b_u5”,“@“\]*)\)”))。组[1]。值; 请帮助您的正则表达式需要的是右括号,而不是尖括号。试一试 var result=

我有像b___5这样的文本

结果输出应该是尖括号()之间的内容,即GetSupportURLs

我尝试以下常规经验,但没有运气

var result= Regex.Match("<GetSupportUrls>b__5", @"\<([^>]*)\)").Groups[1].Value;
var result=Regex.Match(“b_u5”,“@“\]*)\)”))。组[1]。值;

请帮助

您的正则表达式需要的是右括号,而不是尖括号。试一试

var result= Regex.Match("<GetSupportUrls>b__5", @"<([^>]*)>").Groups[1].Value;
var result=Regex.Match(“b_uu5”,@“]*)>”。组[1]。值;

(顺便说一句,您不需要转义
字符)。

您的正则表达式正在寻找一个右括号,而不是尖括号。试一试

var result= Regex.Match("<GetSupportUrls>b__5", @"<([^>]*)>").Groups[1].Value;
var result=Regex.Match(“b_uu5”,@“]*)>”。组[1]。值;

(顺便说一下,您不需要转义
字符)。

您的文本似乎是XML,因此我建议使用System.XML来处理数据

尝试以下方法:

 void string GetSupportUrls()
 {
     var doc = new XmlDocument();
     doc.Load (...);  // your text
     return doc
       .SelectSingleNode("GetSupportUrls")
       .InnerText;
 }

看起来您的文本是XML,所以我建议使用System.XML来处理数据

尝试以下方法:

 void string GetSupportUrls()
 {
     var doc = new XmlDocument();
     doc.Load (...);  // your text
     return doc
       .SelectSingleNode("GetSupportUrls")
       .InnerText;
 }
我会使用正则表达式:

(?<=<).*?(?=>)
以及文件:

  // Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) «(?<=<)»
//    Match the character “<” literally «<»
// Match any single character that is not a line break character «.*?»
//    Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
// Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=>)»
//    Match the character “>” literally «>» 
//断言下面的正则表达式可以匹配,匹配在这个位置结束(正向查找)«(?我将使用正则表达式:

(?<=<).*?(?=>)
以及文件:

  // Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) «(?<=<)»
//    Match the character “<” literally «<»
// Match any single character that is not a line break character «.*?»
//    Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
// Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=>)»
//    Match the character “>” literally «>» 

//断言下面的正则表达式可以匹配,匹配结束于此位置(正向回溯)«(?编程不是基于运气,出了什么问题?您忘记匹配结束括号。“b__5”,“@“\]*)\>\)”编程不是基于运气,出了什么问题?您忘记匹配结束括号。“b_5”,“@“\]*)\>\)”它可能不是XML,而是编译器为LINQ表达式(或类似的东西)生成的方法,即使是XML,代码也会获取节点内的文本,而不是他想要的节点名。如果OP想要节点,则使用“OuterText”而不是“InnerText”另外,我认为在Xml上使用正则表达式是一种不好的做法。如果数据是XML,则使用System.XML;如果是纯文本,则应使用正则表达式。正则表达式无法轻松处理内部文本中的A;它可以合法地存在于任何元素中。正如我所说,它可能不是XML:)。它可能不是XML,而是编译器为LINQ表达式(或类似的东西)生成的方法,即使它是XML,您的代码也会获取节点内的文本,而不是他想要的节点名。如果OP想要节点,则使用“OuterText”而不是“InnerText”另外,我认为在Xml上使用正则表达式是一种不好的做法。如果数据是XML,则使用System.XML;如果是纯文本,则应使用正则表达式。正则表达式无法轻松处理内部文本中的A;它可以合法地存在于任何元素中。正如我所说,它可能不是XML:)。如果文本中存在多个标记,则此操作失败。如果文本中存在多个标记,则此操作失败。