2012-12-27 190 views
1

簡短版本:我試圖構建正則表達式以查明一個字符串是否包含「0,0,0,0」。我所做的每一個嘗試都只是將每個字符作爲匹配返回,而不是引號內的完整字符串。正則表達式模式在VB.NET中無法正常工作

我想在VB.NET的文本框內的字符串中找到某些文本。我的問題是,不是返回一個匹配,而是返回字符串中的每個字符作爲匹配。現在通常我會認爲這是我的正則表達式的一個問題,但是由於我已經驗證它應該與一些在線工具一起工作,所以我不是100%確定的。

我想匹配的字符串是:

0,0,0,0 

我試圖找到看起來像這樣的匹配字符串:

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

我使用了一個名爲FastColoredTextBox控制,它允許爲特定的字符串設置顏色樣式和其他自定義樣式的範圍。以下是我通常如何添加樣式範圍。

目前,我添加了讓單詞可點擊的功能,所以我試圖讓正則表達式爲我想要點擊的字符串構建匹配。例如:

這裏是正則表達式。

​​

當用戶點擊正在使用正則表達式(實施例的此上文)設置爲一個範圍的話,它使字點擊。當用戶點擊單詞時,它會選擇正則表達式中指定的整個範圍。除了這個返回每個「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 

對不起,我只是希望有人能幫我解開我的神祕。

+1

你能否添加一個問號,它很難看到你實際要問什麼。 –

+0

RegEx在哪裏?我知道你覺得RegEx是正確的,但如果它匹配單個字符而不是整個字符串,那幾乎肯定是RegEx。 –

+0

我知道你是從邁克爾來的,這正是我的想法。我也嘗試過Vjays正則表達式,我也收到單個字符。你可以建議正則表達式來查找該字符串是否包含0,0,0,0? – user1632018

回答

0

我沒有得到你在你提供的代碼塊中如何使用RegEx。嘗試下面的語法(它在C#中)。

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

正則表達式不在代碼塊中,它在我之前添加的代碼片段中。 e.changedrange.setstyle。這些代碼片段在我的textchanged事件中。 – user1632018

+0

感謝您的幫助。我在我的代碼中嘗試了你的正則表達式,並且它返回字符串中的每個0。 – user1632018

+0

你可以給我輸入字符串你的傳球。當我嘗試使用「0.0.0.0.somthing」。它返回「0.0.0.0」 –

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(); 
    } 
} 
0

我用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, 
+0

如果它仍然不工作,請嘗試轉義逗號? – Larry

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 
相關問題