【问题标题】:Regular expression pattern not working correctly in VB.NET正则表达式模式在 VB.NET 中无法正常工作
【发布时间】:2012-12-27 03:45:06
【问题描述】:

简短版:我正在尝试构建正则表达式来确定字符串是否包含“0,0,0,0”。我所做的每一次尝试都只返回每个字符作为匹配项,而不是引号内的完整字符串。

我试图在VB.NET 的文本框内的字符串中查找某些文本。我的问题是,它不是返回一个匹配项,而是将字符串中的每个字符作为匹配项返回。现在通常我会认为这是我的正则表达式的问题,但由于我已经验证它应该可以使用几个在线工具,所以我不是 100% 确定。

我要匹配的字符串是:

0,0,0,0

我试图在其中找到匹配项的字符串如下所示:

Image(0,0,0,0,"Path")

我正在使用一个名为 FastColoredTextBox 的控件,它允许为特定字符串设置颜色样式和其他自定义样式的范围。下面是我通常添加样式范围的方法。

目前,我已经添加了使单词可点击的功能,因此我试图让正则表达式为我想要使其可点击的字符串构建匹配项。例如:

这是正则表达式。

Private Sub tb_textchanged(ByVal sender As System.Object, ByVal e As TextChangedEventArgs)

    ' This is working code to make the word Path clickable in the above string:
    e.ChangedRange.SetStyle(ellipseStyle, "\bPath\b", RegexOptions.IgnoreCase)

    ' When I use these ones it returns each character as a match and not the full string. The mystery...
    e.ChangedRange.SetStyle(ellipseStyle, "0,0,0,0", RegexOptions.IgnoreCase)

    e.ChangedRange.SetStyle(ellipseStyle, "(0,){4}", RegexOptions.IgnoreCase)
End Sub

当用户单击使用正则表达式设置为范围的单词时(上面的示例),它使单词可点击。当用户单击单词时,它会选择正则表达式中指定的整个范围。除了这个返回每个“0”和“,”作为它自己的匹配,因此只返回/选择单个字符。

这是我点击单词的代码以便更好地理解。这不包含正则表达式,上面的 textchanged 事件包含。

Private Sub tb_VisualMarkerClick(sender As Object, e As VisualMarkerEventArgs)
    Dim page As RadPageViewPage = RadPageView1.SelectedPage
    Dim txt As FastColoredTextBox = TryCast(page.Controls(0), FastColoredTextBox)
    txt.Invalidate()
    txt.Selection.Start = New Place((TryCast(e.Marker, RangeMarker).range).Start.iChar, (TryCast(e.Marker, RangeMarker).range).Start.iLine)
    txt.SelectionLength = (TryCast(e.Marker, RangeMarker).range).Text.Length
    Dim ClickedWord As String = (TryCast(e.Marker, RangeMarker).range.Text)
    If ClickedWord = "Path" Then
        Dim ofd As New OpenFileDialog
        ofd.FileName = ""
        ofd.Filter = "Image Files (*.bmp, *.jpg)|*.bmp;*.jpg"
        If ofd.ShowDialog = DialogResult.OK Then

            txt.InsertText(ofd.FileName)
        End If
    ElseIf ClickedWord = "0,0,0,0" Then
        'What I am going to do when found.
    End If
End Sub

抱歉,这篇冗长的帖子,我只是希望有人能帮助我解开我的谜团。

【问题讨论】:

  • 你能加个问号吗,很难看出你到底在问什么。
  • 正则表达式在哪里?我知道您觉得 RegEx 是正确的,但如果它匹配单个字符而不是整个字符串,则几乎可以肯定是 RegEx。
  • 我知道你从迈克尔那里来,这正是我的想法。我也尝试过 Vjays 正则表达式,我也得到了单个字符。你能建议正则表达式来查找字符串是否包含 0,0,0,0 吗?

标签: .net regex vb.net winforms


【解决方案1】:

我不明白你想做什么。

检查字符串是否包含模式,例如0,0,0,0,你可以用这个:

string input = "Image(0,0,0,0,\"Path\")";
string pattern = "0,0,0,0";
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);


Console.WriteLine(input.Contains(pattern)); //true
Console.WriteLine(regex.IsMatch(input)); //true

【讨论】:

    【解决方案2】:

    我不知道您在提供的代码块中如何使用RegEx。试试下面的语法(它在 C# 中)。

    System.Text.RegularExpressions.Regex.Split("Input string", "([0.]+)")
    

    【讨论】:

    • 正则表达式不在代码块中,而是在我之前添加的 sn-ps 中。 e.changedrange.setstyle。那些 sn-ps 在我的 textchanged 事件中。
    • 感谢您的帮助。我在我的代码中尝试了你的正则表达式,它返回字符串中的每个 0。
    • 你能给我你的输入字符串吗?正如我尝试使用“0.0.0.0.somthing”一样。它返回“0.0.0.0”
    • 这是我要在全文中找到的字符串。"0,0,0,0" 这些是逗号。
    • 试图看看这个:Image(0,0,0,0,"Path") 或任何其他字符串是否包含这个:0,0,0,0
    【解决方案3】:

    查看此控制台应用程序。

    class Program
    {
        static void Main(string[] args)
        {
            string sPattern = "0,0,0,0";
            string s = "i am looking for  0,0,0,0";
    
            if (System.Text.RegularExpressions.Regex.IsMatch(
                    s, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
            {
                System.Console.WriteLine("  (match for '{0}' found)", sPattern);
            }
            else
            {
                System.Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
    

    【讨论】:

      【解决方案4】:

      我用过My Regex Tester。生成的代码是:

      Imports System.Text.RegularExpressions
      Module Module1
          Sub Main()
              Dim sourcestring As String = "Replace with your source string"
              Dim re As Regex = New Regex("(0,){4}", RegexOptions.IgnoreCase)
              Dim mc As MatchCollection = re.Matches(sourcestring)
              Dim mIdx As Integer = 0
              For Each m As Match In mc
                  For groupIdx As Integer = 0 To m.Groups.Count - 1
                      Console.WriteLine("[{0}][{1}] = {2}", _
                                        mIdx, _
                                        re.GetGroupNames(groupIdx), _
                                        m.Groups(groupIdx).Value)
                  Next
                  mIdx = mIdx + 1
              Next
          End Sub
      End Module
      

      输入文字:

      Image(0,0,0,0,"Path")
      

      输出结果:

      [0][0] = 0,0,0,0,
      [0][1] = 0,
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-25
        • 2016-05-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多