【问题标题】:vb.net regex.matches not returning all matchesvb.net regex.matches 不返回所有匹配项
【发布时间】:2013-01-30 01:35:24
【问题描述】:
Dim pattern As String = "^[ \t]*(\d+)[ \t]*(#[^#]*#)?[ \t]*(\w+)[ \t]?(.*)$"
Dim sentence As String = "9 #Left# Itema Desc"

For Each match As Match In Regex.Matches(sentence, pattern)
  Console.WriteLine("Found '{0}' at position {1}", match.Value, match.Index)
Next

只返回:

在位置 0 找到“9 #Left# Itema Desc”

使用 Expresso 返回测试上述 vb 模式:

1:9

2:#左#

3:意达

4:描述

此外,这个 PHP 正则表达式还返回四项:

preg_match_all('/^[ \t]*(\d+)[ \t]*(#[^#]*#)?[ \t]*(\w+)[ \t]?(.*)$/m', $in, $matches, PREG_SET_ORDER);

我做错了什么??

提前致谢!

感谢 Ark-kun,确实我的问题是组 - 这是有效的代码:

Dim pattern As String = "^[ \t]*(\d+)[ \t]*(#[^#]*#)?[ \t]*(\w+)[ \t]?(.*)$"
Dim sentence As String = "9 #Left# Itema Desc"

Dim match As Match = Regex.Match(sentence, pattern)
If match.Success Then
  Console.WriteLine("Matched text: {0}", match.Value)
    For ctr As Integer = 1 To match.Groups.Count - 1
      Console.WriteLine("   Group {0}:  {1}", ctr, match.Groups(ctr).Value)
    Next
 End If

【问题讨论】:

    标签: php regex vb.net preg-match-all


    【解决方案1】:

    结果在逻辑上是正确的。您已经编写了“整行”正则表达式,而 Regex.Matches 方法找到了一个匹配项 - 整行。 您可能想要的是match.Captures 属性:http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.group.captures.aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-22
      • 1970-01-01
      • 2011-08-17
      • 2020-01-30
      • 1970-01-01
      • 2019-10-10
      • 1970-01-01
      相关资源
      最近更新 更多