首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

正則運算的問題,该如何解决

2012-06-05 
正則運算的問題问题如下:字符串:plum * pear * BALL要将以上字符串转成下面的正则regex :plum & .* pe

正則運算的問題
问题如下:

字符串:  
"plum * pear * BALL"

要将以上字符串转成下面的正则


regex :  
plum & ".*" pear & ".*" BALL


.*代表的是任何字符
 
做这样的正则我的应用是用来搜索匹配类似下面的集合 
 
plum IS pear ON BALL
plum ARE pear ON BALL
 
下面是我目前写的代码  

VB.NET code
 Public Sub main6()        Dim regex As Regex = New Regex(" ")         ' Split on hyphens.        Dim substrings() As String = regex.Split("plum * pear * BALL")        Dim c As String = vbNullString        For Each match As String In substrings            If match = "*" Then                match = "'.*'"            End If            'Me.RichTextBox1.Text += match & vbCrLf            c += match & " & "        Next        Dim c1 As String = c.Trim(" ")        c1 = c1.Trim("&")        c1 = c1.Trim(" ")        Dim regex1 As Regex = New Regex(c1)        Me.RichTextBox1.Text = c1    End Sub   


目前这样处理出来的regex 会变成

VB.NET code
New Regex(plum & '.*' pear & '.*' BALL)



这样的正则做匹配 好像会有问题呢? 本来应该是 " 符号 只能用 ' 符号 ..
 
可是又不晓得如何将 '.*' 改成 ".*" 组进正则里面.
 
请问各位大大有无解法?


[解决办法]
Replace不就行了
 string strreplace = "'.*'";
 strreplace = strreplace.Replace("'", @"""");

热点排行