I've done a lot of searching on this but still can't quite put it all together.
我已经做了很多关于这个的搜索,但仍然不能完全把它放在一起。
I'm trying create an Excel VBA program that populates a spreadsheet based on a user inputting regular expressions so that I can process the files with other vba programs.
我正在尝试创建一个Excel VBA程序,根据用户输入正则表达式填充电子表格,以便我可以使用其他vba程序处理这些文件。
So for example, if I want to populate a folder with all Autodesk Inventor file types , I would use:
例如,如果我想填充包含所有Autodesk Inventor文件类型的文件夹,我会使用:
.*\.(iam|ipt|ipn|idw)
and from what I have read, if I want a regex to skip a file in a folder OR containing a string, I would use something like:
根据我的阅读,如果我想要一个正则表达式跳过文件夹中的文件或包含字符串,我会使用类似的东西:
(?iOldVersions)
but like I mentioned, I am having trouble putting this together so that it is a single reg ex call -- and also, if there are multiple strings that I want it to skip (ie; the folders OldVersions and Legacy)
但就像我提到的那样,我无法将它们放在一起以便它是一个单一的注册调用 - 而且,如果有多个字符串我希望它跳过(即;文件夹OldVersions和Legacy)
I think I would like to keep it as regex although I'm guessing I could also use wScript.Shell (or whatever that object is) but It would be nice to just get familiar with regular expressions for now.
我想我想保留它作为正则表达式,虽然我猜我也可以使用wScript.Shell(或任何对象)但是现在只是熟悉正则表达式会很好。
The code I am using is the same from this post, but instead I added a parameter to pass the pattern to the top level code by pulling it from a cell in excel.
我正在使用的代码在这篇文章中是相同的,但我添加了一个参数,通过从excel中的单元格中提取模式将模式传递给*代码。
List files of certain pattern using Excel VBA
使用Excel VBA列出某些模式的文件
Again, any help would be greatly appreciated!
再次,任何帮助将不胜感激!
Thanks again, all!
全部再次感谢!
Edit: Latest attempt....
编辑:最新尝试....
Private Sub FindPatternMatchedFiles()
objFile = "C:\OldVersions\TestFile.iam"
Dim objRegExp As Object
Set objRegExp = CreateObject("VBScript.RegExp")
'objRegExp.Pattern = "(.*\.(iam|ipt|ipn|idw))(?!(\z))."
objRegExp.Pattern = "(^((?!OldVersions).)*$)(.*\.(iam|ipt|ipn|idw))"
objRegExp.IgnoreCase = True
res = objRegExp.test(objFile)
MsgBox (res)
'Garbage Collection
Set objRegExp = Nothing
End Sub
1 个解决方案
#1
2
To exclude matching strings having \OldVersions\
or \Legacy\
, just add anchors and a negative lookahead at the start:
要排除具有\ OldVersions \或\ Legacy \的匹配字符串,只需在开头添加锚点和否定前瞻:
^(?!.*\\(?:OldVersions|Legacy)\\).*\.(?:iam|ipt|ipn|idw)$
See the regex demo
请参阅正则表达式演示
Details:
-
^
- start of string -
(?!.*\\(?:OldVersions|Legacy)\\)
- a negative lookahead failing the match if there is\
+ eitherOldVersions
orLegacy
+\
after 0+ chars other than\r
and\n
(.*
). -
.*
- 0+ chars other than\r
and\n
, as many as possible, up to the last... -
\.
- literal.
-
(?:iam|ipt|ipn|idw)
- one of the alternatives in the non-capturing group -
$
- end of string.
^ - 字符串的开头
(?!。* \\(?:OldVersions | Legacy)\\) - 如果除了\ r和\ n之外的0 +字符之后有+ + OldVersions或Legacy + \,那么负面前瞻会使匹配失败。
。* - 除了\ r和\ n之外的0 +字符,尽可能多,直到最后...
\。 - 字面意思
(?:iam | ipt | ipn | idw) - 非捕获组中的替代方案之一
$ - 结束字符串。
#1
2
To exclude matching strings having \OldVersions\
or \Legacy\
, just add anchors and a negative lookahead at the start:
要排除具有\ OldVersions \或\ Legacy \的匹配字符串,只需在开头添加锚点和否定前瞻:
^(?!.*\\(?:OldVersions|Legacy)\\).*\.(?:iam|ipt|ipn|idw)$
See the regex demo
请参阅正则表达式演示
Details:
-
^
- start of string -
(?!.*\\(?:OldVersions|Legacy)\\)
- a negative lookahead failing the match if there is\
+ eitherOldVersions
orLegacy
+\
after 0+ chars other than\r
and\n
(.*
). -
.*
- 0+ chars other than\r
and\n
, as many as possible, up to the last... -
\.
- literal.
-
(?:iam|ipt|ipn|idw)
- one of the alternatives in the non-capturing group -
$
- end of string.
^ - 字符串的开头
(?!。* \\(?:OldVersions | Legacy)\\) - 如果除了\ r和\ n之外的0 +字符之后有+ + OldVersions或Legacy + \,那么负面前瞻会使匹配失败。
。* - 除了\ r和\ n之外的0 +字符,尽可能多,直到最后...
\。 - 字面意思
(?:iam | ipt | ipn | idw) - 非捕获组中的替代方案之一
$ - 结束字符串。