mickeym1970
mickeym1970

Reputation: 49

VBScript RegExp with Characters + or -

I cannot seem to figure this out. I need the below to spit out "Market Value"

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

Upvotes: 2

Views: 184

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626929

You may simplify the regex based replacement by using a regex that matches parentheses with any chars inside other than parentheses, enclosed with 0+ whitespaces, and remove the match with RegExp.Replace method directly:

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

See the \s*\([^()]+\)\s* regex demo.

Details

  • \s* - 0+ whitespaces
  • \( - a (
  • [^()]+ - 1 or more (replace with * to match 0 or more) chars other than ( and )
  • \) - a )
  • \s*- 0+ whitespaces

Note that with this regex, objRegExp.IgnoreCase = True does not do anything meaningful and can be removed.

Upvotes: 2

Jan
Jan

Reputation: 43169

You need to add the + to the character class:

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

See a demo on regex101.com.

Upvotes: 2

Related Questions