Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > using split to extract characters

Reply
Thread Tools

using split to extract characters

 
 
zenny lenny
Guest
Posts: n/a
 
      05-01-2007
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

 
Reply With Quote
 
 
 
 
Mirco Wahab
Guest
Posts: n/a
 
      05-01-2007
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:
>
> 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.
 
Reply With Quote
 
 
 
 
Mirco Wahab
Guest
Posts: n/a
 
      05-01-2007
Mirco Wahab wrote:
> 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:
>> xyz3r4abc
>> xyzm9zabc
>> xyzuukabc

> 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.
 
Reply With Quote
 
zenny lenny
Guest
Posts: n/a
 
      05-01-2007
On May 1, 9:20 am, Glenn Jackman <(E-Mail Removed)> wrote:
> 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:

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

>
> Using split: ($data) = split /xyz|abc/;
>
> But why use split? If you know you need a substring, use substr:
> $data = substr $_, 3, 3;
>
> --
> Glenn Jackman
> "You can only be young once. But you can always be immature." -- Dave Barry



Perfect! Thank you!

 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      05-01-2007
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:
>
> 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



 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      05-01-2007
zenny lenny <(E-Mail Removed)> 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:


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


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
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
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
a split is not a split Dumbell Computer Support 3 03-09-2009 10:45 PM
How do i extract vidios when winrar wont extract them??? help plzzzzzzzz smuttdog@sc.rr.com Computer Support 2 12-23-2007 07:03 AM
How can I split database results with ExecuteReader and Split? needin4mation@gmail.com ASP .Net 2 05-05-2006 10:36 PM
split on '' (and another for split -1) trans. (T. Onoma) Ruby 10 12-28-2004 06:36 AM
Small inconsistency between string.split and "".split Carlos Ribeiro Python 11 09-17-2004 05:57 PM



Advertisments