regular expression question

C

Craig Buchanan

I have a string in the format "name" <address> that i would like to split
into an array of two values. name should be the first value, address the
second value. what does my regex pattern need to be? If the regex doesn't
find occurances of two double quotes and an occurance of < and an occurance
of >, will i get a null string array?

btw, is there a difference between:

dim X() as string
and
dim X as string() ?

Thanks,

Craig Buchanan
 
R

Richard T. Edwards

Dim MYString As String

Dim Tr() As String

Dim l As String

Dim r As String

MYString = "Technical Support"

Tr = MYString.Split(" ")

l = Tr(0)

r = Tr(1)

MsgBox(l)

MsgBox(r)
 
J

Jay B. Harlow [MVP - Outlook]

Craig,
I have a string in the format "name" <address> that i would like to split
into an array of two values.
You can parse the values, however you will need to manually build the array.
what does my regex pattern need to be?
You can use the following RegEx to parse the string:


Dim ex As New Regex("^\""(?<name>.*)\"" \<(?<address>.*)\>$")

Dim match As Match = ex.Match("""Herman Munster"" <1313 Mocking Bird
Lane>")

Debug.WriteLine(match.Groups("name"), "name")
Debug.WriteLine(match.Groups("address"), "address")
If the regex doesn't
find occurances of two double quotes and an occurance of < and an occurance
of >, will i get a null string array?
You will not have a match, so yes you will have an empty string returned.


btw, is there a difference between:

dim X() as string and dim X as string() ?
Nothing, they are both an array of Strings, the first is useful when you are
defining a string scalar & array on the same line, while the second is
needed for function & property return values.

Dim y, x() as String

Public Function ReturnStringArray() As String()

Hope this helps
Jay
 
C

Craig Buchanan

thanks a lot Jay!

how would the pattern need look if the name wasn't "" delimited. for
example, Herman Munster <1313 Mocking Bird Lane> ? Perhaps find the first
space before the <, then take everything up to that?

Craig
 
J

Jay B. Harlow [MVP - Outlook]

Craig,
It would look the same except it would not include the ",

Dim ex As New Regex("^(?<name>.*) \<(?<address>.*)\>$")

Dim match As Match = ex.Match("Herman Munster <1313 Mocking Bird
Lane>")

Debug.WriteLine(Match.Groups("name"), "name")
Debug.WriteLine(Match.Groups("address"), "address")


The following site provides a good overview of regular expressions:

http://www.regular-expressions.info/

While this site provides the syntax specifically supported by .NET:

http://msdn.microsoft.com/library/d...l/cpconRegularExpressionsLanguageElements.asp

Hope this helps
Jay
 
C

Craig Buchanan

Jay-

Wow, you're quite good. Two final questions:

1). Is it possible to create a dummy group? For instance, if the string
is <1313 Mocking Bird Lane>, create a pattern that will create a group for
name, assigning 'Unknown' to the group?

2). If I have a string (e-mail address removed), and I want to create a group
named address if there is an @ in the midst of the text.

Thanks so much for your time.

BTW, I used to do this extraction with vbscript, but regex is so much easier
and reliable.

Craig
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top