#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Location
    Boston, MA
    Posts
    5
    Rep Power
    0

    Extract String Within Parenthesis


    I have the following string of characters in the body of an email:

    (name)This is a line.

    I want to extract the "name" from the parens. What is the easiest way to go about this? The characters in parens are always going to be alpha, no numbers.
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,960
    Rep Power
    9397
    The "easiest way to go about" it depends on the language.

    You're posting in the regex forum so I can only assume you're looking for a regex answer.
    Code:
    /\([a-z]+\)/
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Location
    Boston, MA
    Posts
    5
    Rep Power
    0
    I'm using VBScript and it didn't seem to like the above regex very much. Below is a code snippet that is taking what is in parenthesis:

    Code:
    Set objRegEx = CreateObject("VBScript.RegExp")
    Set objRegEx2 = CreateObject("VBScript.RegExp")
    strTest="(Word)This is a test"
    
    objRegEx.Pattern = "([A-Z][a-z]*)"
    Set colMatches = objRegEx.Execute(strTest)
    If colMatches.Count > 0 Then
            For Each strMatch In colMatches
                MsgBox strMatch
            Next
    End If
    My problem now? I want to take everything AFTER the word in parens (so "This is a test"). I tried:
    Code:
    objRegEx.Pattern = "[^([A-Z][a-z]*)]"
    Set colMatches = objRegEx.Execute(strTest)
    If colMatches.Count > 0 Then
            For Each strMatch In colMatches
                MsgBox strMatch
            Next
    End If
    To no avail... any advice?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0
    I don't know VBScript too well these days so forgive me any errors that I might include in regards to that, but I think the following might help:

    Code:
    Set objRegEx = CreateObject("VBScript.RegExp")
    objRegEx.IgnoreCase = True
    objRegEx.Pattern = "\(([a-z]+?)\)(.+)"
    
    strTest="(Word)This is a test"
    
    Set colMatches = objRegEx.Execute(strTest)
    
    If colMatches.Count == 2 Then
        wordInParens = colMatches(0)
        wordOutsideParens = colMatches(1)
    End If

IMN logo majestic logo threadwatch logo seochat tools logo