带字符+或 - 的VBScript RegExp

时间:2017-11-15 21:31:48

标签: regex vbscript

我似乎无法弄清楚这一点。我需要以下内容来吐出市场价值"

Function removeScenarioTags (strtoclean)
    Dim objRegExp, outputStr
    Set objRegExp = New Regexp

    objRegExp.IgnoreCase = True
    objRegExp.Global     = True
    objRegExp.Pattern    = " ?\([0-9a-zA-Z ]+\) ?"          

    Set objMatch  = objRegExp.Execute( strtoclean )
    corrected_row = strtoclean
    For Each myMatch in objMatch
        matched_value  = myMatch.Value                   
        corrected_row  = replace(corrected_row, matched_value, "")  
    Next
    removeScenarioTags = corrected_row
End Function

'----------------MAIN------------------------------------------

after_clean = removeScenarioTags("Market Value (steep+50)")
msgbox after_clean

2 个答案:

答案 0 :(得分:2)

您需要将+添加到字符类:

objRegExp.Pattern = " ?\([0-9a-zA-Z+]+\) ?"

请参阅a demo on regex101.com

答案 1 :(得分:2)

您可以使用与括号匹配的正则表达式与括号内的任何字符匹配来简化基于正则表达式的替换,并用0+空格括起来,并直接用RegExp.Replace方法删除匹配:

Function removeScenarioTags(strtoclean)
    Dim objRegExp, outputStr
    Set objRegExp = New Regexp

    objRegExp.IgnoreCase = True
    objRegExp.Global = True
    objRegExp.Pattern = "\s*\([^()]+\)\s*"

    removeScenarioTags = objRegExp.Replace(strtoclean, "")
End Function

请参阅\s*\([^()]+\)\s*正则表达式演示。

<强>详情

  • \s* - 0+ whitespaces
  • \( - (
  • [^()]+ - 1个或更多(替换为*以匹配0个或更多)除()以外的字符
  • \) - )
  • \s* - 0+ whitespaces

请注意,使用此正则表达式时,objRegExp.IgnoreCase = True不会执行任何有意义的操作,并且可以删除。