Option Explicit Dim s As String Private Sub Command1_Click() Open "C:\123.txt" For Input As #1 s = InputB(LOF(1), #1) Close #1 s = StrConv(s, vbUnicode) s = Replace(s, "<li>", "<br>") s = Replace(s, "</li>", "</br>") Open "C:\123.txt" For Output As #1 Print #1, s Close #1 End Sub
注意:我是用<li></li>测试的。你若用<i></i>测试,改过来就是了。
UPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUP
[Quote=引用 3 楼 syssz 的回复:] VB code Option Explicit Dim s As String Private Sub Command1_Click() Open "C:\123.txt" For Input As #1 s = InputB(LOF(1), #1) Close #1 s = StrConv(s, vbUnicode) s = Replace(s, "<li>", "<br>") s = Replace(s, "</li>", "</br>")/[Quote]不是<li></li>分别用<br></br>替换 而是在<li>……</li>中出现的<br>用其他的字符替换(比如说用<asdf>替换<br>) 并且,必须是相邻的<li></li>它们中间不能再有<li></li>出现
UP UPUP UPUPUP UPUPUPUP UPUPUPUPUP UPUPUPUPUPUP UPUPUPUPUPUPUP
Private Sub Command1_Click() Dim s, t As String Dim tstr1, tstr2 As String '用来替换的字符串 被替换的字符串 Dim tstr3, tstr4 As String '查找的开头字符串 查找的结尾字符串 s = Trim(Text1.Text) tstr1 = "sad" tstr2 = "happy" tstr3 = "<li>" tstr4 = "</li>" s = restr(s, tstr1, tstr2, tstr3, tstr4) Text2.Text = s End Sub Private Function restr(ByVal a As String, ByVal b As String, ByVal c As String, ByVal f1 As String, ByVal f2 As String) Dim t1, t2 As Integer Dim s0, s1, s2 As String t1 = InStr(a, f1) t2 = InStrRev(a, f2) If t2 > t1 And t1 > 0 Then s1 = Left(a, t1 + Len(f1) - 1) s2 = Right(a, Len(a) - t2 + 1) s0 = Mid(a, t1 + Len(f1), t2 - t1 - Len(f1)) s0 = restr(s0, b, c, f1, f2) restr = s1 & s0 & s2 Else restr = Replace(a, b, c) End If End Function
综合10楼 和12楼的想法写了一段: Private Function restr(ByVal a As String, ByVal b As String, ByVal c As String, ByVal f1 As String, ByVal f2 As String) Dim t1, t2, t3, t4 As Integer Dim s0, s1, s2, temp, temp2 As String t2 = InStr(a, f2) t1 = InStrRev(a, f1, t2 - 1) If t2 <> 0 And t1 <> 0 Then s0 = Mid(a, t1 + Len(f1), t2 - t1 - Len(f1)) s1 = Left(a, t1 + Len(f1) - 1) s2 = Right(a, Len(a) - t2 + 1) temp = Replace(s0, b, c) a = s1 & temp & s2 t3 = t2
Do While t2 <> 0 t2 = InStr(t2 + Len(f2), a, f2) t1 = InStrRev(a, f1, t2 - 1) If t1 <> 0 And t1 > t3 Then s0 = Mid(a, t1 + Len(f1), t2 - t1 - Len(f1)) s1 = Left(a, t1 + Len(f1) - 1) s2 = Right(a, Len(a) - t2 + 1) temp = Replace(s0, b, c) a = s1 & temp & s2 t3 = t2 End If Loop restr = a End If
End FunctionPrivate Sub command1_click() Dim s, t As String Dim tstr1, tstr2 As String '用来替换的字符串 被替换的字符串s1 & restr & Dim tstr3, tstr4 As String '查找的开头字符串 查找的结尾字符串 s = Trim(Text1.Text) tstr1 = "<br>" tstr2 = "happy" tstr3 = "<i>" tstr4 = "</i>" s = restr(s, tstr1, tstr2, tstr3, tstr4) Text2.Text = s End Sub
For Each match In matchs strData = Replace(strData, match, Replace(match, "<br>", "xxx")) Next Debug.Print strData End Sub 建议楼主列出原始需求,打个比方,比如你要到A处,但是你只知道在C处左拐一小段路就到了,然后你就打听C在哪里,结果可能会绕更多的路,倒不如直接问A在哪里。
Dim s As String
Private Sub Command1_Click()
Open "C:\123.txt" For Input As #1
s = InputB(LOF(1), #1)
Close #1
s = StrConv(s, vbUnicode)
s = Replace(s, "<li>", "<br>")
s = Replace(s, "</li>", "</br>")
Open "C:\123.txt" For Output As #1
Print #1, s
Close #1
End Sub
VB code
Option Explicit
Dim s As String
Private Sub Command1_Click()
Open "C:\123.txt" For Input As #1
s = InputB(LOF(1), #1)
Close #1
s = StrConv(s, vbUnicode)
s = Replace(s, "<li>", "<br>")
s = Replace(s, "</li>", "</br>")/[Quote]不是<li></li>分别用<br></br>替换
而是在<li>……</li>中出现的<br>用其他的字符替换(比如说用<asdf>替换<br>)
并且,必须是相邻的<li></li>它们中间不能再有<li></li>出现
UPUP
UPUPUP
UPUPUPUP
UPUPUPUPUP
UPUPUPUPUPUP
UPUPUPUPUPUPUP
Private Sub Command1_Click()
Dim s, t As String
Dim tstr1, tstr2 As String '用来替换的字符串 被替换的字符串
Dim tstr3, tstr4 As String '查找的开头字符串 查找的结尾字符串
s = Trim(Text1.Text)
tstr1 = "sad"
tstr2 = "happy"
tstr3 = "<li>"
tstr4 = "</li>"
s = restr(s, tstr1, tstr2, tstr3, tstr4)
Text2.Text = s
End Sub
Private Function restr(ByVal a As String, ByVal b As String, ByVal c As String, ByVal f1 As String, ByVal f2 As String)
Dim t1, t2 As Integer
Dim s0, s1, s2 As String
t1 = InStr(a, f1)
t2 = InStrRev(a, f2)
If t2 > t1 And t1 > 0 Then
s1 = Left(a, t1 + Len(f1) - 1)
s2 = Right(a, Len(a) - t2 + 1)
s0 = Mid(a, t1 + Len(f1), t2 - t1 - Len(f1))
s0 = restr(s0, b, c, f1, f2)
restr = s1 & s0 & s2
Else
restr = Replace(a, b, c)
End If
End Function
2.找到</li>的位置后反向查找<li>
3.记下1、2的位置,截取1、2之间的字符串
4.在3截取的字符串中查找<br>,找到了就用<asdf>替换,再把这段字符串跟原来字符串的前面部分、后面部分组装起来,再跳到下一步。如果找不到<br>就什么都不做直接跳到下一步。
5.找下一个</li>,由步骤1开始循环,直到原字符串最后一个</li>
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
Private Function restr(ByVal a As String, ByVal b As String, ByVal c As String, ByVal f1 As String, ByVal f2 As String)
Dim t1, t2, t3, t4 As Integer
Dim s0, s1, s2, temp, temp2 As String t2 = InStr(a, f2)
t1 = InStrRev(a, f1, t2 - 1)
If t2 <> 0 And t1 <> 0 Then
s0 = Mid(a, t1 + Len(f1), t2 - t1 - Len(f1))
s1 = Left(a, t1 + Len(f1) - 1)
s2 = Right(a, Len(a) - t2 + 1)
temp = Replace(s0, b, c)
a = s1 & temp & s2
t3 = t2
Do While t2 <> 0
t2 = InStr(t2 + Len(f2), a, f2)
t1 = InStrRev(a, f1, t2 - 1)
If t1 <> 0 And t1 > t3 Then
s0 = Mid(a, t1 + Len(f1), t2 - t1 - Len(f1))
s1 = Left(a, t1 + Len(f1) - 1)
s2 = Right(a, Len(a) - t2 + 1)
temp = Replace(s0, b, c)
a = s1 & temp & s2
t3 = t2
End If
Loop
restr = a
End If
End FunctionPrivate Sub command1_click()
Dim s, t As String
Dim tstr1, tstr2 As String '用来替换的字符串 被替换的字符串s1 & restr &
Dim tstr3, tstr4 As String '查找的开头字符串 查找的结尾字符串
s = Trim(Text1.Text)
tstr1 = "<br>"
tstr2 = "happy"
tstr3 = "<i>"
tstr4 = "</i>"
s = restr(s, tstr1, tstr2, tstr3, tstr4)
Text2.Text = s
End Sub
<i>和</i>必须是临近的
Private Sub Form_Load()
Dim strData$, s$, s1$
Dim reg As Object
Dim matchs As Object, match As Object
strData = "<i>11asdfadfgdfg<i>22sdfgdsfgertew<br>xhdjrtua</i>sdfgdfgfghfg</i>asdf<br>sdfgsdgf<i>"
Set reg = CreateObject("vbscript.regexp")
reg.Global = True
reg.IgnoreCase = True
reg.Pattern = "<i>[\s\S]*?</i>"
Set matchs = reg.Execute(strData)
For Each match In matchs
strData = Replace(strData, match, Replace(match, "<br>", "xxx"))
Next
Debug.Print strData
End Sub
建议楼主列出原始需求,打个比方,比如你要到A处,但是你只知道在C处左拐一小段路就到了,然后你就打听C在哪里,结果可能会绕更多的路,倒不如直接问A在哪里。
strData = "<i>11asdfadfgdfg<i>22sdfgdsfgertew<br>xhdjrtua</i>sdfgd<br>fgfghfg</i>asdf<br>sdfgsdgf<i>"
Private Sub Cmd1_Click()
Dim iStr1 As String, istr2 As String, fst1 As Integer, fst2 As Integer iStr1 = "<i>11asdfadfgdfg<i>22sdfgdsfgertew<br>xhdjrtua</i>sdfgdfgfghfg</i>asdf<br>sdfgsdgf<i>"
fst2 = InStr(iStr1, "</i>")
fst1 = InStrRev(iStr1, "<i>", fst2)
istr2 = Left(iStr1, fst1 + 2) & _
Replace(Mid(iStr1, fst1 + 3, fst2 - fst1 - 3), "<br>", "^^^^", , , vbTextCompare) & _
Right(iStr1, Len(iStr1) - fst2 + 1)
MsgBox iStr1 & vbNewLine & istr2
End Sub
http://topic.csdn.net/u/20100522/18/1AB05F3E-C48D-4C10-8CEB-1916E355763B.html
不知道是不是楼主问的