Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex VBA中的正则表达式:将复杂字符串拆分为多个标记?_Regex_Excel_Vba - Fatal编程技术网

Regex VBA中的正则表达式:将复杂字符串拆分为多个标记?

Regex VBA中的正则表达式:将复杂字符串拆分为多个标记?,regex,excel,vba,Regex,Excel,Vba,我正在尝试使用Excel2000/2003将mmCIF蛋白质文件中的一行解析为单独的标记。最坏的情况可能是这样的: token1 token2 "token's 1a',1b'" 'token4"5"' 12 23.2 ? . 'token' tok'en to"ken 应成为以下代币: token1 token2 token's 1a',1b' (note: the double quotes have disappeared) token4"5" (note: the sing

我正在尝试使用Excel2000/2003将mmCIF蛋白质文件中的一行解析为单独的标记。最坏的情况可能是这样的:

token1 token2 "token's 1a',1b'" 'token4"5"' 12 23.2 ? . 'token' tok'en to"ken
应成为以下代币:

token1  
token2  
token's 1a',1b' (note: the double quotes have disappeared)  
token4"5" (note: the single quotes have disappeared)  
12  
23.2  
?  
.  
token (note: the single quotes have disappeared)  
to'ken  
to"ken  
我想看看正则表达式是否可以将此类行拆分为令牌?

可以执行以下操作:

您需要在VBA项目中引用“Microsoft VBScript正则表达式5.5”,然后

Private Sub REFinder(PatternString As String, StringToTest As String)
    Set RE = New RegExp

    With RE
        .Global = True
        .MultiLine = False
        .IgnoreCase = False
        .Pattern = PatternString
    End With

    Set Matches = RE.Execute(StringToTest)

    For Each Match In Matches
        Debug.Print Match.Value & " ~~~ " & Match.FirstIndex & " - " & Match.Length & " = " & Mid(StringToTest, Match.FirstIndex + 1, Match.Length)

        ''#You get a submatch for each of the other possible conditions (if using ORs)
        For Each Item In Match.SubMatches
            Debug.Print "Submatch:" & Item
        Next Item
        Debug.Print
    Next Match

    Set RE = Nothing
    Set Matches = Nothing
    Set Match = Nothing
    Set SubMatch = Nothing
End Sub

Sub DoIt()
    ''#This simply splits by space...
    REFinder "([.^\w]+\s)|(.+$)", "Token1 Token2 65.56"
End Sub
这显然只是一个非常简单的例子,因为我对RegExp不太了解,它更只是向您展示如何在VBA中完成它(您可能还想做一些比调试更有用的事情。使用生成的标记打印!)。恐怕我得把写RegExp表达式的工作留给别人了

西蒙

拼图不错。谢谢

这个模式(下面的aptt)将标记分开,但我不知道如何删除外部引号

tallpaul()产生:

 token1
 token2
 "token's 1a',1b'"
 'token4"5"'
 12
 23.2
 ?
 .
 'token'
 tok'en
 to"ken
如果您能找出如何丢失外部报价,请让我们知道。 这需要引用“Microsoft VBScript正则表达式”才能工作


有六个问题被回答,没有一个被接受的答案,你似乎不太关心你在这里的伴侣。事实上,你离真相已经不远了,贝西里乌斯。我昨晚发布了这个问题,没有收到StackOverflow给我的电子邮件通知我的问题已经得到了回答,我收到了你的smartass回复。完全不需要也不被赏识。贝里萨利斯,我明白你在说什么。我不知道接受回答的协议。我向所有相关人员道歉。然而,据我估计,你本可以更圆滑一点。我已经接受了之前所有问题的答案。谢谢,西蒙。我熟悉regexpvb脚本选项。我就是想不出一个reg表达式!:-)我使用的是OPtion Explicit,因此我必须将Dim语句添加到VBA中。我将继续使用正确的RegExp。。。我忘记了你的能力,所以你帮了我!太棒了,马克!我写了很多更干净的代码来处理这种情况。我不是像你一样的reg exp专家,但如果我能想出如何让它删除引号,我会添加一条评论。现在,我将测试每个令牌的两端,看看它是否包含相同的引号分隔符,并相应地删除。谢谢Paul。我不是专业人士,但我在计划和评估统计模型(goodplan.ca)方面所做的工作让我在长期缺席后重新使用Excel和VBA。我确实有一些好书。如果你打算做很多这方面的工作,我推荐John Green等人的“Excel 2007 VBA程序员参考”,当然还有MSDN网站。虽然还没有测试过,但我想你应该在括号前加上反斜杠,这样可以抓住整个引用的令牌。这不会“抓取”它,而是简单地对它进行分组,以确保其操作顺序低于|(以防万一,因为这里不需要操作顺序)。然后在引号内添加一组新的(抓取)paren,将引号保留在外面,这样它们就不会在匹配中返回。例如,aPatt=“\(\s'([^']+)”(?=\s)\)\124;…我做了一些测试,我认为反斜杠不起作用,所以(忘记反斜杠)只需添加一组新的内部参数,也可以只捕获标记,而不使用外部引号。
Option Explicit
''returns a list of matches
Function RegExpTest(patrn, strng)
   Dim regEx   ' Create variable.
   Set regEx = New RegExp   ' Create a regular expression.
   regEx.Pattern = patrn   ' Set pattern.
   regEx.IgnoreCase = True   ' Set case insensitivity.
   regEx.Global = True   ' Set global applicability.
   Set RegExpTest = regEx.Execute(strng)   ' Execute search.
End Function

Function tallpaul() As Boolean
    Dim aString As String
    Dim aPatt As String
    Dim aMatch, aMatches

    '' need to pad the string with leading and trailing spaces.
    aString = " token1 token2 ""token's 1a',1b'"" 'token4""5""' 12 23.2 ? . 'token' tok'en to""ken "
    aPatt = "(\s'[^']+'(?=\s))|(\s""[^""]+""(?=\s))|(\s[\w\?\.]+(?=\s))|(\s\S+(?=\s))"
    Set aMatches = RegExpTest(aPatt, aString)

    For Each aMatch In aMatches
          Debug.Print aMatch.Value
    Next
    tallpaul = True
End Function