注册.exp:子串和整个串 [英] Reg. exp: substring AND whole string
问题描述
我正在使用 VBscript.我有这个字符串:
I'm working in VBscript. I have this string:
hello
<!-- @@include file="filename" try="folder1" default="folder2" -->
world
我想提取文件"、文件名、尝试"、文件夹、默认"、另一个文件夹,并且我想从 < 中获取整个字符串.!-- 到 --> .
I want to extract "file", the filename, "try", the folder, "default", the other folder, AND I want to get the whole string, from < ! -- to -- > .
这个正则表达式给了我三个匹配:
This regular expression gets me three matches:
(try|default|file)(="([^"]+)")
try、default 和 file 片段,每个片段中都有子匹配项用于各个段.这很好,但无论我在上面的表达式中添加什么来尝试获取整个字符串,例如
The try, default, and file pieces, with submatches in each for the individual segments. That's great, but no matter what I add to the above expression to try and get the entire string as well, e.g.
(!-- @@include (try|default|file)(="([^"]+)") -->)
我从三场比赛变成了一场比赛,丢失了 try/file/default 部分.可能有多个@@include,所以我需要整个匹配加上子匹配,所以我确保用正确的内容替换正确的标签.
I go from three matches to just one, losing the try/file/default pieces. There might be more than one @@include, so I need the whole match plus the submatches so I make sure to replace the right tag with the right content.
我不知道如何改变表情,救命!
I can't figure how to alter the expression, help!
推荐答案
strSample = "<!-- @@include file=""filename"" try=""folder1"" default=""folder2"" -->" & vbCrLf & "<!-- @@include default=""default first"" file=""filename at the end"" -->"
' this regex will match @@include lines which have the exact parameters set
With CreateObject("VBScript.RegExp")
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = "<!-- @@include file=""(.*?)"" try=""(.*?)"" default=""(.*?)"" -->"
Set objMatches = .Execute(strSample)
For Each objMatch In objMatches
MsgBox "whole string:" & vbCrLf & objMatch.Value & vbCrLf & "file: " & objMatch.SubMatches(0) & vbCrLf & "try: " & objMatch.SubMatches(1) & vbCrLf & "default: " & objMatch.SubMatches(2), , "exact"
Next
End With
' these nested regexes will match @@include lines which have any of three parameters in arbitrary order within line
With CreateObject("VBScript.RegExp")
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = "<!-- @@include (?:(?:try|default|file)="".*?"" )*?-->"
For Each objLineMatch In .Execute(strSample)
MsgBox "whole string:" & vbCrLf & objLineMatch.Value, , "arbitrary"
With CreateObject("VBScript.RegExp")
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = "(try|default|file)=""(.*?)"""
For Each objPropMatch In .Execute(objLineMatch.Value)
MsgBox "Name: " & objPropMatch.SubMatches(0) & vbCrLf & "Value: " & objPropMatch.SubMatches(1), , "arbitrary"
Next
End With
Next
End With
这篇关于注册.exp:子串和整个串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!