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

C

ChrisC

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
 
S

sln

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
 
C

ChrisC

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.
 
S

sln

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top