Regex references

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • spamsickle@gmail.com

    #1

    Regex references

    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(N ameTextBox.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 $3 are
    invalid characters.

    My hunch is that $1 and $3 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.

  • Cor Ligthert [MVP]

    #2
    Re: Regex references

    SpamSimple,

    Why don't you use in this case
    LastIndexOf(cha r)

    http://msdn2.microsoft.com/en-us/library/0w96zd3d.aspx

    And then the substring

    The Regex is not the fastest method in Net.

    I hope this helps,

    Cor


    <spamsickle@gma il.com> schreef in bericht
    news:1147055656 .872513.43390@g 10g2000cwb.goog legroups.com...[color=blue]
    >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(N ameTextBox.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 $3 are
    > invalid characters.
    >
    > My hunch is that $1 and $3 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.
    >[/color]


    Comment

    • Göran Andersson

      #3
      Re: Regex references

      The IsMatch method only checks for a match, it doesn't store the
      matches. The $ references are only used in a replacement string.

      Use the Match method to get a Match object. The Groups property in the
      Match object is a collection of the captures.


      spamsickle@gmai l.com wrote:[color=blue]
      > 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(N ameTextBox.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 $3 are
      > invalid characters.
      >
      > My hunch is that $1 and $3 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.
      >[/color]

      Comment

      • david

        #4
        Re: Regex references

        On 2006-05-08, spamsickle@gmai l.com <spamsickle@gma il.com> wrote:[color=blue]
        > 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(N ameTextBox.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 $3 are
        > invalid characters.
        >
        > My hunch is that $1 and $3 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.[/color]

        You need to create a Match object.

        Dim m As Match = Regex.Match(Nam eTextBox.Text, pattern)

        If m.Success Then
        FirstName = m.Groups(1).Val ue
        LastName = m.Groups(3).Val ue

        PS. .Net has some nice additions for regex such as named capture and
        non-capture, consider:

        (?<firstName>^\ w+)\s(?:\w\s)?( ?<lastName>\w+) $

        ' Now you can use
        FirstName = m.Groups("first Name").Value









        [color=blue]
        >[/color]

        Comment

        • housebuyer@gmail.com

          #5
          Re: Regex references

          Thanks to everyone who posted, it's working like a charm now.

          The main reason I'd been using Perl was for the regex capabilities, and
          I still prefer it for my scripting work. For interactive applications
          like the one I'm tackling now, however, I found Perl/Tk to be a lot
          more difficult to use than the .NET IDE. I think going forward, a
          significant number of the applications that I would have formerly done
          in Perl are going to be done with .NET.

          Thanks again to all.

          Comment

          Working...