using split to extract characters

Z

zenny lenny

This should be relatively simple but I can't figure it out. I need to
extract the center 3 characters from this data. The text xyz and abc
do not change:

xyz3r4abc
xyzm9zabc
xyzuukabc

I tried several variations on this command, using xyz and abc as
delimiters. The problem is, if any of the characters x,y,z,a,b,c
appear in the data they will be treated as delimiters, so this won't
work.

($data)=split /[xyz,abc]+/

Is there any way to treat xyz and abc as strings? I tried double and
single quotes but that didn't work.

tia
 
M

Mirco Wahab

zenny said:
This should be relatively simple but I can't figure it out. I need to
extract the center 3 characters from this data. The text xyz and abc
do not change:

xyz3r4abc
xyzm9zabc
xyzuukabc

I tried several variations on this command, using xyz and abc as
delimiters. The problem is, if any of the characters x,y,z,a,b,c
appear in the data they will be treated as delimiters, so this won't
work.

($data)=split /[xyz,abc]+/

Is there any way to treat xyz and abc as strings? I tried double and
single quotes but that didn't work.

try to extract it by matching a regular
expression on it, like /(?<=xyz)...(?=abc)/

example:

...
my @data = qw'
xyz3r4abc
xyzm9zabc
xyzuukabc
';

@data = map /(?<=xyz)...(?=abc)/g, @data;

print "@data";


Regards

M.
 
M

Mirco Wahab

Mirco said:
try to extract it by matching a regular
expression on it, like /(?<=xyz)...(?=abc)/

Oh, if you want 'split', then you could use
the | (or) pattern alternation operator:

...
my @data = qw'
xyz3r4abc
xyzm9zabc
xyzuukabc
';

print split /xyz|abc/ for @data;



Regards

M.
 
Z

zenny lenny

At 2007-05-01 11:59AM, "zenny lenny" wrote:


This should be relatively simple but I can't figure it out. I need to
extract the center 3 characters from this data. The text xyz and abc
do not change:

I tried several variations on this command, using xyz and abc as
delimiters. The problem is, if any of the characters x,y,z,a,b,c
appear in the data they will be treated as delimiters, so this won't
work.
($data)=split /[xyz,abc]+/
Is there any way to treat xyz and abc as strings? I tried double and
single quotes but that didn't work.

Using split: ($data) = split /xyz|abc/;

But why use split? If you know you need a substring, use substr:
$data = substr $_, 3, 3;


Perfect! Thank you!
 
J

Jürgen Exner

zenny said:
This should be relatively simple but I can't figure it out. I need to
extract the center 3 characters from this data. The text xyz and abc
do not change:

xyz3r4abc
xyzm9zabc
xyzuukabc

I tried several variations on this command, using xyz and abc as
delimiters. The problem is, if any of the characters x,y,z,a,b,c
appear in the data they will be treated as delimiters, so this won't
work.

($data)=split /[xyz,abc]+/

When it comes to REs people seem to get so excited that they loose their
common sense. Why on earth do you want to wield the big and certainly
powerful RE gun when a very modest and simple substr() does the job
perfectly fine?

$data = substr ($_, 3, 3);

jue
 
J

Jens Thoms Toerring

zenny lenny said:
This should be relatively simple but I can't figure it out. I need to
extract the center 3 characters from this data. The text xyz and abc
do not change:

I tried several variations on this command, using xyz and abc as
delimiters. The problem is, if any of the characters x,y,z,a,b,c
appear in the data they will be treated as delimiters, so this won't
work.
($data)=split /[xyz,abc]+/
Is there any way to treat xyz and abc as strings? I tried double and
single quotes but that didn't work.

Why don't you use e.g.

( $data = $_ ) =~ s/^xyz(.*?)abc$/$1/;

(assuming that the text is stored in '$_')? Or, if it's always
three characters in the middle with three in front of them

$data = substr $_, 3, 3;

If you insist on using split try

$data = ( split /^xyz|abc$/ )[ 1 ];

The '^' and '$' are important since they ensure that the match
on 'xyz' only happens at the very start and that on 'abc' only
at the very end, so it will also work with text like "xyzxyzabc"
or "xyzabcabc".
Regards, Jens
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top