1

简短版本:我正在尝试构建正则表达式以找出字符串是否包含“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

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

4

4 回答 4

0

I have used My Regex Tester. The code generated is:

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

Input text:

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

Output Result:

[0][0] = 0,0,0,0,
[0][1] = 0,
于 2012-12-27T07:52:26.073 回答
0

我不明白你想做什么。

要检查字符串是否包含模式,例如 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
于 2012-12-27T11:48:42.557 回答
0

查看此控制台应用程序。

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();
    }
}
于 2012-12-27T07:28:23.847 回答
0

我没有RegEx在您提供的代码块中了解您的使用方式。试试下面的语法(它在 C# 中)。

System.Text.RegularExpressions.Regex.Split("Input string", "([0.]+)")
于 2012-12-27T05:01:17.587 回答