Regex Issues

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Labosh

    #1

    Regex Issues

    I have the following System.Text.Reg ularExpressions .Regex that is supposed
    to remove this predefined list of garbage characters from contact names that
    come in on import files :

    Dim _dropContactGar bage As New Regex( _
    "([" & Chr(0) & "-" & Chr(31) & "]+)|" & _
    "([" & Chr(33) & "-" & Chr(38) & "]+)|" & _
    "([" & Chr(40) & "-" & Chr(44) & "]+)|" & _
    "([" & Chr(47) & "-" & Chr(47) & "]+)|" & _
    "([" & Chr(58) & "-" & Chr(64) & "]+)|" & _
    "([" & Chr(91) & "-" & Chr(96) & "]+)|" & _
    "([" & Chr(123) & "-" & Chr(127) & "]+)|" & _
    "([" & Chr(152) & "]+)|" & _
    "([" & Chr(155) & "-" & Chr(159) & "]+)|" & _
    "([" & Chr(166) & "-" & Chr(224) & "]+)|" & _
    "([" & Chr(226) & "-" & Chr(255) & "]+)")

    We use it like this:

    value = _dropContactGar bage.Replace(va lue, "")

    But the Regex constructor is throwing an ArgumentExcepti on whose Message
    property says only "Parse ([". There is no inner exception. Normally, if I
    have a string expression that's wrong, I would Console.WriteLi ne() it. But
    in this case, it doesn't WriteLine correctly, because some of the characters
    in the expression are control characters, so what it displays is not
    visually correct.

    I have slaved over this issue for hours and hours and I can only guess that
    one of the items must be escaped with a "\" or something, but I cannot
    figure it out. I have already been all over the MSDN help topics for the
    Regex Class.

    Help?

    --
    Peace & happy computing,

    Mike Labosh, MCSD
    "After very careful consideration, I have come
    to the conclusion that this new system SUCKS"
    -- General Barringer, from WARGAMES


  • Some Guy

    #2
    Re: Regex Issues

    I think you're having problems with all those &'s between the brakets.
    Maybe?

    "([Chr(0) & "-" & Chr(31) ]+)|"


    "Mike Labosh" <mlabosh@hotmai l.com> wrote in message
    news:eneIdvonFH A.3540@TK2MSFTN GP10.phx.gbl...[color=blue]
    >I have the following System.Text.Reg ularExpressions .Regex that is supposed
    >to remove this predefined list of garbage characters from contact names
    >that come in on import files :
    >
    > Dim _dropContactGar bage As New Regex( _
    > "([" & Chr(0) & "-" & Chr(31) & "]+)|" & _
    > "([" & Chr(33) & "-" & Chr(38) & "]+)|" & _
    > "([" & Chr(40) & "-" & Chr(44) & "]+)|" & _
    > "([" & Chr(47) & "-" & Chr(47) & "]+)|" & _
    > "([" & Chr(58) & "-" & Chr(64) & "]+)|" & _
    > "([" & Chr(91) & "-" & Chr(96) & "]+)|" & _
    > "([" & Chr(123) & "-" & Chr(127) & "]+)|" & _
    > "([" & Chr(152) & "]+)|" & _
    > "([" & Chr(155) & "-" & Chr(159) & "]+)|" & _
    > "([" & Chr(166) & "-" & Chr(224) & "]+)|" & _
    > "([" & Chr(226) & "-" & Chr(255) & "]+)")
    >
    > We use it like this:
    >
    > value = _dropContactGar bage.Replace(va lue, "")
    >
    > But the Regex constructor is throwing an ArgumentExcepti on whose Message
    > property says only "Parse ([". There is no inner exception. Normally, if
    > I have a string expression that's wrong, I would Console.WriteLi ne() it.
    > But in this case, it doesn't WriteLine correctly, because some of the
    > characters in the expression are control characters, so what it displays
    > is not visually correct.
    >
    > I have slaved over this issue for hours and hours and I can only guess
    > that one of the items must be escaped with a "\" or something, but I
    > cannot figure it out. I have already been all over the MSDN help topics
    > for the Regex Class.
    >
    > Help?
    >
    > --
    > Peace & happy computing,
    >
    > Mike Labosh, MCSD
    > "After very careful consideration, I have come
    > to the conclusion that this new system SUCKS"
    > -- General Barringer, from WARGAMES
    >[/color]


    Comment

    • Mike Labosh

      #3
      Re: Regex Issues

      >> Dim _dropContactGar bage As New Regex( _[color=blue][color=green]
      >> "([" & Chr(0) & "-" & Chr(31) & "]+)|" & _
      >> "([" & Chr(33) & "-" & Chr(38) & "]+)|" & _
      >> "([" & Chr(40) & "-" & Chr(44) & "]+)|" & _
      >> "([" & Chr(47) & "-" & Chr(47) & "]+)|" & _
      >> "([" & Chr(58) & "-" & Chr(64) & "]+)|" & _
      >> "([" & Chr(91) & "-" & Chr(96) & "]+)|" & _
      >> "([" & Chr(123) & "-" & Chr(127) & "]+)|" & _
      >> "([" & Chr(152) & "]+)|" & _
      >> "([" & Chr(155) & "-" & Chr(159) & "]+)|" & _
      >> "([" & Chr(166) & "-" & Chr(224) & "]+)|" & _
      >> "([" & Chr(226) & "-" & Chr(255) & "]+)")[/color][/color]
      [color=blue]
      >I think you're having problems with all those &'s between the brakets.
      >Maybe?
      >
      > "([Chr(0) & "-" & Chr(31) ]+)|"[/color]

      No. It's not the concatenation. The dumb thing *used to work*, but they
      want me to change a couple of the character ranges. So all I have done is
      changed a couple of the character codes passed to the Chr() function. Now
      it's b0rken.

      What I am currently attempting is to create a Regex from each single line of
      my OP so I can find which one is causing the issue, then perhaps I can
      determine a workaround.
      --
      Peace & happy computing,

      Mike Labosh, MCSD
      "Musha ring dum a doo dum a da!" -- James Hetfield


      Comment

      • jg

        #4
        Re: Regex Issues

        make sure the new character code does not special meaning in regex. IF they
        do, use the escape prefix before the " & chr...

        Sorry, I don't know the details, but I am sure you can look it up in msdn
        under regex

        "Mike Labosh" <mlabosh@hotmai l.com> wrote in message
        news:e3NQsDqnFH A.3760@TK2MSFTN GP15.phx.gbl...[color=blue][color=green][color=darkred]
        >>> Dim _dropContactGar bage As New Regex( _
        >>> "([" & Chr(0) & "-" & Chr(31) & "]+)|" & _
        >>> "([" & Chr(33) & "-" & Chr(38) & "]+)|" & _
        >>> "([" & Chr(40) & "-" & Chr(44) & "]+)|" & _
        >>> "([" & Chr(47) & "-" & Chr(47) & "]+)|" & _
        >>> "([" & Chr(58) & "-" & Chr(64) & "]+)|" & _
        >>> "([" & Chr(91) & "-" & Chr(96) & "]+)|" & _
        >>> "([" & Chr(123) & "-" & Chr(127) & "]+)|" & _
        >>> "([" & Chr(152) & "]+)|" & _
        >>> "([" & Chr(155) & "-" & Chr(159) & "]+)|" & _
        >>> "([" & Chr(166) & "-" & Chr(224) & "]+)|" & _
        >>> "([" & Chr(226) & "-" & Chr(255) & "]+)")[/color][/color]
        >[color=green]
        >>I think you're having problems with all those &'s between the brakets.
        >>Maybe?
        >>
        >> "([Chr(0) & "-" & Chr(31) ]+)|"[/color]
        >
        > No. It's not the concatenation. The dumb thing *used to work*, but they
        > want me to change a couple of the character ranges. So all I have done is
        > changed a couple of the character codes passed to the Chr() function. Now
        > it's b0rken.
        >
        > What I am currently attempting is to create a Regex from each single line
        > of my OP so I can find which one is causing the issue, then perhaps I can
        > determine a workaround.
        > --
        > Peace & happy computing,
        >
        > Mike Labosh, MCSD
        > "Musha ring dum a doo dum a da!" -- James Hetfield
        >[/color]


        Comment

        • Mike Labosh

          #5
          Re: Regex Issues

          > make sure the new character code does not special meaning in regex. IF[color=blue]
          > they do, use the escape prefix before the " & chr...[/color]

          That's what I'm trying to do. Each line of the expression seems to work by
          itself. So I am now trying varying combinations.
          [color=blue]
          > Sorry, I don't know the details, but I am sure you can look it up in msdn
          > under regex[/color]

          heh. If you saw all the MSDN printouts on my desk, you would hurt me for
          killing trees :)

          --
          Peace & happy computing,

          Mike Labosh, MCSD
          "Musha ring dum a doo dum a da!" -- James Hetfield


          Comment

          • Chris Burgess

            #6
            Re: Regex Issues

            It barks at me until I remove this line:
            "([" & Chr(155) & "-" & Chr(159) & "]+)|" & _

            I'm not sure why.

            "Mike Labosh" <mlabosh@hotmai l.com> wrote in message
            news:eneIdvonFH A.3540@TK2MSFTN GP10.phx.gbl...[color=blue]
            >I have the following System.Text.Reg ularExpressions .Regex that is supposed
            >to remove this predefined list of garbage characters from contact names
            >that come in on import files :
            >
            > Dim _dropContactGar bage As New Regex( _
            > "([" & Chr(0) & "-" & Chr(31) & "]+)|" & _
            > "([" & Chr(33) & "-" & Chr(38) & "]+)|" & _
            > "([" & Chr(40) & "-" & Chr(44) & "]+)|" & _
            > "([" & Chr(47) & "-" & Chr(47) & "]+)|" & _
            > "([" & Chr(58) & "-" & Chr(64) & "]+)|" & _
            > "([" & Chr(91) & "-" & Chr(96) & "]+)|" & _
            > "([" & Chr(123) & "-" & Chr(127) & "]+)|" & _
            > "([" & Chr(152) & "]+)|" & _
            > "([" & Chr(155) & "-" & Chr(159) & "]+)|" & _
            > "([" & Chr(166) & "-" & Chr(224) & "]+)|" & _
            > "([" & Chr(226) & "-" & Chr(255) & "]+)")
            >
            > We use it like this:
            >
            > value = _dropContactGar bage.Replace(va lue, "")
            >
            > But the Regex constructor is throwing an ArgumentExcepti on whose Message
            > property says only "Parse ([". There is no inner exception. Normally, if
            > I have a string expression that's wrong, I would Console.WriteLi ne() it.
            > But in this case, it doesn't WriteLine correctly, because some of the
            > characters in the expression are control characters, so what it displays
            > is not visually correct.
            >
            > I have slaved over this issue for hours and hours and I can only guess
            > that one of the items must be escaped with a "\" or something, but I
            > cannot figure it out. I have already been all over the MSDN help topics
            > for the Regex Class.
            >
            > Help?
            >
            > --
            > Peace & happy computing,
            >
            > Mike Labosh, MCSD
            > "After very careful consideration, I have come
            > to the conclusion that this new system SUCKS"
            > -- General Barringer, from WARGAMES
            >[/color]


            Comment

            • Chris Burgess

              #7
              Re: Regex Issues

              parsing "([>-Y]+)|" - [x-y] range in reverse order.



              "Chris Burgess" <cburgess@conve rse.com> wrote in message
              news:%23GoQWArn FHA.3540@TK2MSF TNGP10.phx.gbl. ..[color=blue]
              > It barks at me until I remove this line:
              > "([" & Chr(155) & "-" & Chr(159) & "]+)|" & _
              >
              > I'm not sure why.
              >
              > "Mike Labosh" <mlabosh@hotmai l.com> wrote in message
              > news:eneIdvonFH A.3540@TK2MSFTN GP10.phx.gbl...[color=green]
              >>I have the following System.Text.Reg ularExpressions .Regex that is supposed
              >>to remove this predefined list of garbage characters from contact names
              >>that come in on import files :
              >>
              >> Dim _dropContactGar bage As New Regex( _
              >> "([" & Chr(0) & "-" & Chr(31) & "]+)|" & _
              >> "([" & Chr(33) & "-" & Chr(38) & "]+)|" & _
              >> "([" & Chr(40) & "-" & Chr(44) & "]+)|" & _
              >> "([" & Chr(47) & "-" & Chr(47) & "]+)|" & _
              >> "([" & Chr(58) & "-" & Chr(64) & "]+)|" & _
              >> "([" & Chr(91) & "-" & Chr(96) & "]+)|" & _
              >> "([" & Chr(123) & "-" & Chr(127) & "]+)|" & _
              >> "([" & Chr(152) & "]+)|" & _
              >> "([" & Chr(155) & "-" & Chr(159) & "]+)|" & _
              >> "([" & Chr(166) & "-" & Chr(224) & "]+)|" & _
              >> "([" & Chr(226) & "-" & Chr(255) & "]+)")
              >>
              >> We use it like this:
              >>
              >> value = _dropContactGar bage.Replace(va lue, "")
              >>
              >> But the Regex constructor is throwing an ArgumentExcepti on whose Message
              >> property says only "Parse ([". There is no inner exception. Normally,
              >> if I have a string expression that's wrong, I would Console.WriteLi ne()
              >> it. But in this case, it doesn't WriteLine correctly, because some of the
              >> characters in the expression are control characters, so what it displays
              >> is not visually correct.
              >>
              >> I have slaved over this issue for hours and hours and I can only guess
              >> that one of the items must be escaped with a "\" or something, but I
              >> cannot figure it out. I have already been all over the MSDN help topics
              >> for the Regex Class.
              >>
              >> Help?
              >>
              >> --
              >> Peace & happy computing,
              >>
              >> Mike Labosh, MCSD
              >> "After very careful consideration, I have come
              >> to the conclusion that this new system SUCKS"
              >> -- General Barringer, from WARGAMES
              >>[/color]
              >
              >[/color]


              Comment

              • Jay B. Harlow [MVP - Outlook]

                #8
                Re: Regex Issues

                Mike,
                Rather then literally using Chr(0), Chr(31), Chr(33), ..., I would recommend
                the RegEx Character Escape sequences.

                http://msdn.microsoft.com/library/de...terescapes.asp

                Something like:

                ' With ASCII character escapes
                Dim _dropContactGar bage As New Regex( _
                "([\x00-\x1F]+)|" & _
                "([\x21-\x26]+)|" & _
                "([\x28-\x2C]+)|" & _
                ...

                Of course you may have problems with Chr(128) & above, as Chr(128) is an
                ANSI char code, while Regex expects ASCII and/or Unicode. As you know ASCII
                is 7 bit (0 to 127) & Unicode in RegEx needs 4 digits (\u0000).

                ' with Unicode character escapes
                Dim _dropContactGar bage As New Regex( _
                "([\u0000-\u001F]+)|" & _
                "([\u0021-\u0026]+)|" & _
                "([\u0028-\u002C]+)|" & _


                It might be "easier" if you used a the predefined character classes (\s \w
                \W \s ...) instead:

                http://msdn.microsoft.com/library/de...terclasses.asp

                Something like:
                Dim _dropContactGar bage As New Regex("\W")

                Which says match any nonword character...

                Expresso & RegEx Workbench both have wizards of varying degrees to help you
                build your expression, plus they allow you to test your expressions, also
                the analyzer/interpreter in each is rather handy.

                Expresso:


                RegEx Workbench:


                tutorial & reference on using regular expressions:
                At Regular-Expressions.info you will find a wide range of in-depth information about a powerful search pattern language called regular expressions.


                The MSDN's documentation on regular expressions:
                http://msdn.microsoft.com/library/de...geElements.asp

                Hope this helps
                Jay



                "Mike Labosh" <mlabosh@hotmai l.com> wrote in message
                news:eneIdvonFH A.3540@TK2MSFTN GP10.phx.gbl...
                |I have the following System.Text.Reg ularExpressions .Regex that is supposed
                | to remove this predefined list of garbage characters from contact names
                that
                | come in on import files :
                |
                | Dim _dropContactGar bage As New Regex( _
                | "([" & Chr(0) & "-" & Chr(31) & "]+)|" & _
                | "([" & Chr(33) & "-" & Chr(38) & "]+)|" & _
                | "([" & Chr(40) & "-" & Chr(44) & "]+)|" & _
                | "([" & Chr(47) & "-" & Chr(47) & "]+)|" & _
                | "([" & Chr(58) & "-" & Chr(64) & "]+)|" & _
                | "([" & Chr(91) & "-" & Chr(96) & "]+)|" & _
                | "([" & Chr(123) & "-" & Chr(127) & "]+)|" & _
                | "([" & Chr(152) & "]+)|" & _
                | "([" & Chr(155) & "-" & Chr(159) & "]+)|" & _
                | "([" & Chr(166) & "-" & Chr(224) & "]+)|" & _
                | "([" & Chr(226) & "-" & Chr(255) & "]+)")
                |
                | We use it like this:
                |
                | value = _dropContactGar bage.Replace(va lue, "")
                |
                | But the Regex constructor is throwing an ArgumentExcepti on whose Message
                | property says only "Parse ([". There is no inner exception. Normally, if
                I
                | have a string expression that's wrong, I would Console.WriteLi ne() it.
                But
                | in this case, it doesn't WriteLine correctly, because some of the
                characters
                | in the expression are control characters, so what it displays is not
                | visually correct.
                |
                | I have slaved over this issue for hours and hours and I can only guess
                that
                | one of the items must be escaped with a "\" or something, but I cannot
                | figure it out. I have already been all over the MSDN help topics for the
                | Regex Class.
                |
                | Help?
                |
                | --
                | Peace & happy computing,
                |
                | Mike Labosh, MCSD
                | "After very careful consideration, I have come
                | to the conclusion that this new system SUCKS"
                | -- General Barringer, from WARGAMES
                |
                |


                Comment

                Working...