By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,052 Members | 1,248 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,052 IT Pros & Developers. It's quick & easy.

Regex references

P: n/a
I have a Perl background, so some of what I know in other contexts is
probably getting in the way of what I need to learn now. With that
said, I'm having a problem getting my regex to work as I expect. I
have a string value like "John Q Public" in a textbox called "Name",
and I want to use the regex to split out first name and last name.
Here's what I've coded:

Dim FirstName As String
Dim LastName As String
If Regex.IsMatch(NameTextBox.Text, "(^\w+)\s(\w\s)?(\w+)$")
Then
FirstName = $1
LastName = $3
End If

but the the VB 2005 Express IDE is telling me the $ in $1 and $2 are
invalid characters.

My hunch is that $1 and $2 only make sense inside the Regex namespace,
but I'm not sure what my next step should be. Do I need to create a
Match object to access the values? All the examples I've found on the
web seem to assume that the only reason you'd want to extract values is
to re-arrange them in a replace, but that's not what I have in mind.

May 8 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
SpamSickle,

I have given you an answer in another newsgroup you have posted too.

Cor

<sp********@gmail.com> schreef in bericht
news:11*********************@v46g2000cwv.googlegro ups.com...
I have a Perl background, so some of what I know in other contexts is
probably getting in the way of what I need to learn now. With that
said, I'm having a problem getting my regex to work as I expect. I
have a string value like "John Q Public" in a textbox called "Name",
and I want to use the regex to split out first name and last name.
Here's what I've coded:

Dim FirstName As String
Dim LastName As String
If Regex.IsMatch(NameTextBox.Text, "(^\w+)\s(\w\s)?(\w+)$")
Then
FirstName = $1
LastName = $3
End If

but the the VB 2005 Express IDE is telling me the $ in $1 and $2 are
invalid characters.

My hunch is that $1 and $2 only make sense inside the Regex namespace,
but I'm not sure what my next step should be. Do I need to create a
Match object to access the values? All the examples I've found on the
web seem to assume that the only reason you'd want to extract values is
to re-arrange them in a replace, but that's not what I have in mind.

May 8 '06 #2

P: n/a
I had a little trouble with your regular expression, so I rewrote it:

(^\w+)\s+(?:\w+\s+)*(\w+$)

This accounts for more than one space between parts of the name, and doesn't
group the matches between the beginning and the end. Instead, it puts the
first word into Group 1, and the last into Group 2.

To use it, you call Match on the string, and then get the 2 Groups out of
the Match. This is done using the Match class, which has a Groups
collection. You reference each Group by its index in the Groups. Example:

Dim FirstName As String
Dim LastName As String
Dim r As Regex = new Regex("(^\w+)\s+(?:\w+\s+)*(\w+$)");
Dim match As Match = r.Match(NameTextBox.Text);
FirstName = match.Groups[1].Value
LastName = match.Groups[2].Value

A couple of notes: The Match method will *always* return a Match object
instance. Its Value may be "". The Match will *always* have all of the
Groups in the Regex. If there is nothing in the Group, its Value will be "".

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

<sp********@gmail.com> wrote in message
news:11*********************@v46g2000cwv.googlegro ups.com...
I have a Perl background, so some of what I know in other contexts is
probably getting in the way of what I need to learn now. With that
said, I'm having a problem getting my regex to work as I expect. I
have a string value like "John Q Public" in a textbox called "Name",
and I want to use the regex to split out first name and last name.
Here's what I've coded:

Dim FirstName As String
Dim LastName As String
If Regex.IsMatch(NameTextBox.Text, "(^\w+)\s(\w\s)?(\w+)$")
Then
FirstName = $1
LastName = $3
End If

but the the VB 2005 Express IDE is telling me the $ in $1 and $2 are
invalid characters.

My hunch is that $1 and $2 only make sense inside the Regex namespace,
but I'm not sure what my next step should be. Do I need to create a
Match object to access the values? All the examples I've found on the
web seem to assume that the only reason you'd want to extract values is
to re-arrange them in a replace, but that's not what I have in mind.

May 8 '06 #3

P: n/a
Thank you both, the code is now doing exactly what I'd hoped, and I
have a much better understanding of how to work with regular
expressions in this environment. I really appreciate it.

May 8 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.