Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to use Regex to breakdown a pattern and use the pattern to breakdown a string

Reply
Thread Tools

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

 
 
ChrisC
Guest
Posts: n/a
 
      06-22-2010
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
 
Reply With Quote
 
 
 
 
sln@netherlands.com
Guest
Posts: n/a
 
      06-23-2010
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
 
Reply With Quote
 
 
 
 
ChrisC
Guest
Posts: n/a
 
      06-23-2010
On Jun 22, 10:36*pm, Tad McClellan <ta...@seesig.invalid> wrote:
> ChrisC <gjwp...@gmail.com> 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.
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      06-24-2010
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
 
Reply With Quote
 
ChrisC
Guest
Posts: n/a
 
      06-25-2010
Tad,

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

Regards

Jerry
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Regex testing and UTF8 awarenes or Regex and numeric pattern matching sln@netherlands.com Perl Misc 2 03-10-2009 03:51 AM
String Pattern Matching: regex and Python regex documentation Xah Lee Python 8 09-26-2006 03:24 PM
String Pattern Matching: regex and Python regex documentation Xah Lee Perl Misc 2 09-25-2006 03:15 AM
String Pattern Matching: regex and Python regex documentation Xah Lee Java 1 09-22-2006 07:11 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57