Helpful Information
 
 
Category: Regex Programming
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.

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.

/\([a-z]+\)/

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:



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:


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?

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:


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










privacy (GDPR)