How to use Regex to breakdown a pattern and use the pattern to breakdown a string

Discussion in 'Perl Misc' started by ChrisC, Jun 22, 2010.

  1. ChrisC

    ChrisC Guest

    I will get a user defined patten "XXXXX##', "##XXXXX##", "##XX##XX##",
    "XXX" etc. How to break down this string pattern using Regex and
    apply it to data;

    Pattern "XXXXXXXX##" to break out string "00000078.7\r\n" to get
    "00000078.7";

    Pattern ""##XX##XX##" to break out string "LB78KL.7l\n" to get "78.7";

    etc......

    Or what is the best way to do this?

    Thanks,

    Jerry
     
    ChrisC, Jun 22, 2010
    #1
    1. Advertising

  2. ChrisC

    Guest

    Re: How to use Regex to breakdown a pattern and use the pattern to break down a string

    On Tue, 22 Jun 2010 15:14:45 -0700 (PDT), ChrisC <> wrote:

    >I will get a user defined patten "XXXXX##', "##XXXXX##", "##XX##XX##",
    >"XXX" etc. How to break down this string pattern using Regex and
    >apply it to data;
    >
    >Pattern "XXXXXXXX##" to break out string "00000078.7\r\n" to get
    >"00000078.7";
    >
    >Pattern ""##XX##XX##" to break out string "LB78KL.7l\n" to get "78.7";
    >
    >etc......
    >
    >Or what is the best way to do this?
    >
    >Thanks,
    >
    >Jerry


    I would think long and hard before doing this.

    use strict;
    use warnings;

    my $data = "LB78KL.7l\n";
    my $pat = "##XX##XX##";

    $pat =~ s/(X+)/'(' . '.'x length($1) . ')'/eg;
    $pat =~ tr/#/./;
    print join '', $data =~ /$pat/s;

    -sln
     
    , Jun 23, 2010
    #2
    1. Advertising

  3. ChrisC

    ChrisC Guest

    Re: How to use Regex to breakdown a pattern and use the pattern tobreak down a string

    On Jun 22, 10:36 pm, Tad McClellan <> wrote:
    > ChrisC <> wrote:
    > > I will get a user defined patten "XXXXX##', "##XXXXX##", "##XX##XX##",
    > > "XXX" etc.  How to break down this string pattern using Regex and
    > > apply it to data;

    >
    > That depends entirely on what meaning you assign to
    > the "X" and "#" characters in your pattern language.
    >
    > What meaning do you assign to the "X" and "#" characters in your
    > pattern language?
    >
    > --
    > Tad McClellan
    > email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
    > The above message is a Usenet post.
    > I don't recall having given anyone permission to use it on a Web site.


    Tad,

    I want to keep # data and disregard X data/position in a string that
    is being read from a serial port.
     
    ChrisC, Jun 23, 2010
    #3
  4. ChrisC

    Guest

    Re: How to use Regex to breakdown a pattern and use the pattern to break down a string

    On Wed, 23 Jun 2010 13:53:37 -0700 (PDT), ChrisC <> wrote:

    >I want to keep # data and disregard X data/position in a string that
    >is being read from a serial port.


    As someone said tr/X#/xa/ and unpack would be the way to go.
    However, without an anchor or other frame reference, this will
    do you absolutely no good whatsoever.

    -sln
     
    , Jun 24, 2010
    #4
  5. ChrisC

    ChrisC Guest

    Re: How to use Regex to breakdown a pattern and use the pattern tobreak down a string

    Tad,

    I inverted the pattern, SORRY! It should be "#######XX"

    Regards

    Jerry
     
    ChrisC, Jun 25, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stan
    Replies:
    5
    Views:
    702
    Steven Cheng[MSFT]
    Jan 16, 2004
  2. =?Utf-8?B?dGhlamVmZnJvc3M=?=

    ASP.Net Memory Breakdown

    =?Utf-8?B?dGhlamVmZnJvc3M=?=, May 26, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    1,460
    Kevin Spencer
    May 26, 2005
  3. Xah Lee
    Replies:
    1
    Views:
    972
    Ilias Lazaridis
    Sep 22, 2006
  4. Xah Lee
    Replies:
    8
    Views:
    483
    Ilias Lazaridis
    Sep 26, 2006
  5. Xah Lee
    Replies:
    2
    Views:
    240
    Xah Lee
    Sep 25, 2006
Loading...

Share This Page