我有一個字串如下
p.msonormal0, li.msonormal0, div.msonormal0
{mso-style-name:msonormal;
mso-style-unhide:no;
mso-margin-top-alt:auto;
margin-right:0in;
mso-margin-bottom-alt:auto;
margin-left:0in;
mso-pagination:widow-orphan;
font-size:12.0pt;
font-family:SimSun;
mso-bidi-font-family:Calibri;}
span.wordsection1Char
{mso-style-name:"wordsection1 Char";
mso-style-priority:99;
mso-style-unhide:no;
mso-style-locked:yes;
mso-style-link:wordsection1;
font-family:"MS Mincho";
mso-ascii-font-family:"MS Mincho";
mso-fareast-font-family:"MS Mincho";
mso-hansi-font-family:"MS Mincho";
color:#000099;
mso-fareast-language:JA;}
p.wordsection1, li.wordsection1, div.wordsection1
{mso-style-name:wordsection1;
mso-style-priority:99;
mso-style-unhide:no;
mso-style-link:"wordsection1 Char";
mso-margin-top-alt:auto;
margin-right:0in;
mso-margin-bottom-alt:auto;
margin-left:0in;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"MS Mincho";
mso-bidi-font-family:Calibri;
color:#000099;
mso-fareast-language:JA;}
span.EmailStyle20
{mso-style-type:personal;
mso-style-noshow:yes;
mso-style-unhide:no;
font-family:"Arial Unicode MS",serif;
mso-ascii-font-family:"Arial Unicode MS";
mso-hansi-font-family:"Arial Unicode MS";
color:black;}
我想要使用VBA刪除
span.wordsection1Char
{mso-style-name:"wordsection1 Char";
mso-style-priority:99;
mso-style-unhide:no;
mso-style-locked:yes;
mso-style-link:wordsection1;
font-family:"MS Mincho";
mso-ascii-font-family:"MS Mincho";
mso-fareast-font-family:"MS Mincho";
mso-hansi-font-family:"MS Mincho";
color:#000099;
mso-fareast-language:JA;}
p.wordsection1, li.wordsection1, div.wordsection1
{mso-style-name:wordsection1;
mso-style-priority:99;
mso-style-unhide:no;
mso-style-link:"wordsection1 Char";
mso-margin-top-alt:auto;
margin-right:0in;
mso-margin-bottom-alt:auto;
margin-left:0in;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"MS Mincho";
mso-bidi-font-family:Calibri;
color:#000099;
mso-fareast-language:JA;}
得到以下結果
span.EmailStyle20
{mso-style-type:personal;
mso-style-noshow:yes;
mso-style-unhide:no;
font-family:"Arial Unicode MS",serif;
mso-ascii-font-family:"Arial Unicode MS";
mso-hansi-font-family:"Arial Unicode MS";
color:black;}
我嘗試使用Replace,但那隻對明確、單行有效果...
備註 : span.wordsection1Char之間屬性、值不一定
用VBA跑正則表達式
' 建立正規表示法變數
Dim regEx As New RegExp
' 要匹配與取代的文字資料
Dim oldString As String
oldString = "p.msonormal0, li.msonormal0, div.msonormal0
{mso-style-name:msonormal;
mso-style-unhide:no;
mso-margin-top-alt:auto;
margin-right:0in;
mso-margin-bottom-alt:auto;
margin-left:0in;
mso-pagination:widow-orphan;
font-size:12.0pt;
font-family:SimSun;
mso-bidi-font-family:Calibri;}
span.wordsection1Char
{mso-style-name:"wordsection1 Char";
mso-style-priority:99;
mso-style-unhide:no;
mso-style-locked:yes;
mso-style-link:wordsection1;
font-family:"MS Mincho";
mso-ascii-font-family:"MS Mincho";
mso-fareast-font-family:"MS Mincho";
mso-hansi-font-family:"MS Mincho";
color:#000099;
mso-fareast-language:JA;}
p.wordsection1, li.wordsection1, div.wordsection1
{mso-style-name:wordsection1;
mso-style-priority:99;
mso-style-unhide:no;
mso-style-link:"wordsection1 Char";
mso-margin-top-alt:auto;
margin-right:0in;
mso-margin-bottom-alt:auto;
margin-left:0in;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"MS Mincho";
mso-bidi-font-family:Calibri;
color:#000099;
mso-fareast-language:JA;}
span.EmailStyle20
{mso-style-type:personal;
mso-style-noshow:yes;
mso-style-unhide:no;
font-family:"Arial Unicode MS",serif;
mso-ascii-font-family:"Arial Unicode MS";
mso-hansi-font-family:"Arial Unicode MS";
color:black;}"
' 設定規則,以【不含】emailstyle字樣(不分大小寫)的做替換
regEx.Pattern = "^(?:(?!\bemailstyle).)*\n(?:\s*\n*\r*)(?:\n?\r?)\{(?:[^}]*\r?\n?)\}"
' 替代的文字,把符合上列規則的換成空白(跟刪除一樣意思)
Dim MyReplace As String
MyReplace = ""
' 設定成不分大小寫
regEx.IgnoreCase = True
' 設定全域選項
regEx.Global = True
' 多行
regEx.Multiline = True
' 使用正規表示法匹配並取代文字
If regEx.Test(oldString) Then
Result = regEx.Replace(oldString, MyReplace)
MsgBox ("取代後的結果:" & Result)
Else
MsgBox ("匹配失敗")
End If
(終於搞懂必不包含特定字串的用法,功力又提升一甲子...秒)
重點是這一行規則,很燒腦啊
regEx.Pattern = "^(?:(?!\bemailstyle).)*\n(?:\s*\n*\r*)(?:\n?\r?)\{(?:[^}]*\r?\n?)\}"
試試看result = Mid(source, InStr(1, source, "span.EmailStyle20"))