Regular Express Pattern Help!

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

    #1

    Regular Express Pattern Help!

    We have a string like: 2004\05\05T00:0 0:00

    We would like to remove all "\", any "-" and the T00:00:00 part of the
    string.

    The end result would be 20040505.

    I know that if I use [\\-] as the pattern it will match on everything
    except the final T00 part.

    Could anyone please help us with this pattern.

    TIA

    Drew Mace
  • William Ewart Gladstone

    #2
    Re: Regular Express Pattern Help!


    I would just use a .Replace and SubString methods

    So,

    myString.Replac e("\","").Repla ce("-","").SubString (0,8)


    Drew wrote:
    [color=blue]
    > We have a string like: 2004\05\05T00:0 0:00
    >
    > We would like to remove all "\", any "-" and the T00:00:00 part of the
    > string.
    >
    > The end result would be 20040505.
    >
    > I know that if I use [\\-] as the pattern it will match on everything
    > except the final T00 part.
    >
    > Could anyone please help us with this pattern.
    >
    > TIA
    >
    > Drew Mace[/color]

    --
    w:4

    Comment

    • Bruce Brown

      #3
      Re: Regular Express Pattern Help!

      Hi,

      The following VBScript shows how you might make it work.

      StringToSearch = "2004\5\05T00:0 0:00"
      Set RegularExpressi onObject = New RegExp
      With RegularExpressi onObject
      .Pattern = "\\|T..:..: .."
      .IgnoreCase = True
      .Global = True
      End With
      MsgBox RegularExpressi onObject.Replac e( StringToSearch, "" )

      Of course whether this will work for all your cases depends on how fixed
      the structure of the input is.

      Regards,

      - Bruce.





      "William Ewart Gladstone" <victorian@era. politicos> wrote in message
      news:1309258.EF GjZBvzcP@news.w est.earthlink.n et...[color=blue]
      >
      > I would just use a .Replace and SubString methods
      >
      > So,
      >
      > myString.Replac e("\","").Repla ce("-","").SubString (0,8)
      >
      >
      > Drew wrote:
      >[color=green]
      > > We have a string like: 2004\05\05T00:0 0:00
      > >
      > > We would like to remove all "\", any "-" and the T00:00:00 part of the
      > > string.
      > >
      > > The end result would be 20040505.
      > >
      > > I know that if I use [\\-] as the pattern it will match on everything
      > > except the final T00 part.
      > >
      > > Could anyone please help us with this pattern.
      > >
      > > TIA
      > >
      > > Drew Mace[/color]
      >
      > --
      > w:4
      >[/color]


      Comment

      • Niki Estner

        #4
        Re: Regular Express Pattern Help!

        To match the backslashes and the T... part, you can match "\\|-|(T.*)"
        However, I'd suggest replacing "(\d\d\d\d)\\(\ d\d)\\(\d\d)T\d \d:\d\d:\d\d"
        with "$1$2$3".
        Using this expression you can be sure you won't mess up data that's not
        formatted as expected.

        Niki

        "Drew" <drewjunk@comca st.net> wrote in
        news:e33694b7.0 406140511.49357 2a1@posting.goo gle.com...[color=blue]
        > We have a string like: 2004\05\05T00:0 0:00
        >
        > We would like to remove all "\", any "-" and the T00:00:00 part of the
        > string.
        >
        > The end result would be 20040505.
        >
        > I know that if I use [\\-] as the pattern it will match on everything
        > except the final T00 part.
        >
        > Could anyone please help us with this pattern.
        >
        > TIA
        >
        > Drew Mace[/color]


        Comment

        • Nick Malik

          #5
          Re: Regular Express Pattern Help!

          It would be much easier to read your code if you didn't use regular
          expressions. How about this idea:

          string sourcestring = "2004\05\05T00: 00:00";

          string[] listbits = sourcestring.sp lit('T');
          string[] datebits = listbits[0].split('\');
          string newdate = String.Join(dat ebits);

          *caveat: air code... I didn't compile this in VS first.

          That's pretty easy code, and it will work as long as the original data HAS a
          "T" in it. (Fails otherwise).

          Hope this helps,
          --- N

          "Drew" <drewjunk@comca st.net> wrote in message
          news:e33694b7.0 406140511.49357 2a1@posting.goo gle.com...[color=blue]
          > We have a string like: 2004\05\05T00:0 0:00
          >
          > We would like to remove all "\", any "-" and the T00:00:00 part of the
          > string.
          >
          > The end result would be 20040505.
          >
          > I know that if I use [\\-] as the pattern it will match on everything
          > except the final T00 part.
          >
          > Could anyone please help us with this pattern.
          >
          > TIA
          >
          > Drew Mace[/color]


          Comment

          • Niki Estner

            #6
            Re: Regular Express Pattern Help!

            I have to disagree with you: If you think 3 lines are more readable than 1
            line, that's probably because you never learned regular expressions.

            A few advantages of regexes:
            - if you do understand them they are easier to read than c# code, because:
            - they have no branches, exceptions, loops, etc, everything's
            "straightforwar d" (quite literally)
            - instead of specifying the transformation rules (which are pretty hards to
            understand if you don't know what the source string will look like) you
            specify the input pattern and the target output - that's high-level
            programming;
            - they can be compiled, and are often faster than a c# equivalent (make your
            own timings if you don't trust me)
            - they don't produce exceptions or endless loops due to bad/unexpected data
            - they can easily be separated from the main program and extensively tested
            (e.g. in Expresso)
            - they don't have side-effects, i.e. the pattern can't mess around with any
            global/local variables (yes, this is a BIG source for errors!)
            - using regex'es it's far easier to create "picky" code, that doesn't
            produce bad results from bad input; Your code e.g. would happily take
            "Hello\\how\\ar e\\you\\Today" and transform it to "Hellohowareyou " - I
            wouldn't want that in the "Date" column of my database...
            (plus a few more good reasons I can't think of right now)

            I agree with you that regex'es are often mis-used when "string.spl it" would
            have done the same, but THIS kind of problem is what they have actually been
            MADE for.

            Niki


            "Nick Malik" <nickmalik@hotm ail.nospam.com> wrote in
            news:Tatzc.2427 9$Hg2.13887@att bi_s04...[color=blue]
            > It would be much easier to read your code if you didn't use regular
            > expressions. How about this idea:
            >
            > string sourcestring = "2004\05\05T00: 00:00";
            >
            > string[] listbits = sourcestring.sp lit('T');
            > string[] datebits = listbits[0].split('\');
            > string newdate = String.Join(dat ebits);
            >
            > *caveat: air code... I didn't compile this in VS first.
            >
            > That's pretty easy code, and it will work as long as the original data HAS[/color]
            a[color=blue]
            > "T" in it. (Fails otherwise).
            >
            > Hope this helps,
            > --- N
            >
            > "Drew" <drewjunk@comca st.net> wrote in message
            > news:e33694b7.0 406140511.49357 2a1@posting.goo gle.com...[color=green]
            > > We have a string like: 2004\05\05T00:0 0:00
            > >
            > > We would like to remove all "\", any "-" and the T00:00:00 part of the
            > > string.
            > >
            > > The end result would be 20040505.
            > >
            > > I know that if I use [\\-] as the pattern it will match on everything
            > > except the final T00 part.
            > >
            > > Could anyone please help us with this pattern.
            > >
            > > TIA
            > >
            > > Drew Mace[/color]
            >
            >[/color]


            Comment

            Working...