【问题标题】:How can i match all characters?我怎样才能匹配所有字符?
【发布时间】:2011-01-16 05:41:58
【问题描述】:

例如,我如何匹配 regex.match 中所有可能的字符。

string value4 = (",\"message\":\"all characters\",");
Match[] message = Regex.Matches(docText, @value4)

匹配

,"message":"all characters here",

我试过了

string value4 = (",\"message\":\".\","); 

string value4 = (",\"message\":\"[.]\","); 

string value4 = (",\"message\":\"[.*]\",");

string value4 = (",\"message\":\".*\",");

它们都不起作用。

编辑:

我与,"message":"all characters here", 匹配的值可以在“此处的所有字符”部分中包含任何字符,我想匹配,"message":"all characters here", 的所有实例,忽略第二组引号之间的内容

【问题讨论】:

标签: c# regex match


【解决方案1】:

如果您的值中没有任何引号,您可以使用:

"message":"([^"]*)"

写成

  • "\"message\":\"([^\"]*)\"" - 作为常规字符串文字
  • @"""message"":""([^""]*)""" 作为Verbatim String)

如果你有转义引号,一个选项是这样的,它也允许所有转义字符:

"message":"(([^"\\]|\\.)*)"

写成:

  • "\"message\":\"(([^\"\\\\]|\\\\.)*)\""
  • @"""message"":""(([^""\\]|\\.)*)"""

【讨论】:

  • @user556396 - 请将其编辑到问题中,并解释您要做什么。我显然误解了:P
  • 已编辑,希望更有意义
  • 啊,是的,谢谢你帮我解决这个问题,转义字符阻止了比赛。谢谢。
  • @user556396 - 没问题,很乐意提供帮助。我正在写一个简短的代码示例,但我想你不需要它。代码中有一些奇怪之处 - 例如,Regex.Matches 不返回数组,而是返回 MatchCollection(尽管我认为这只是示例代码)。无论如何,祝你好运!
猜你喜欢
  • 1970-01-01
  • 2014-10-24
  • 2018-05-17
  • 2023-02-02
  • 1970-01-01
  • 2021-05-25
  • 2017-02-25
  • 1970-01-01
  • 2023-02-04
相关资源
最近更新 更多