读书人

正算的,该如何解决

发布时间: 2012-06-05 13:54:06 作者: rapoo

正算的
问题如下:

字符串:
"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("'", @"""");

读书人网 >VB Dotnet

热点推荐