Click here to get back home

regular expression with split goes wrong ?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
regular expression with split goes wrong ? jh3an 03-10-2008
Posted by jh3an on March 10, 2008, 5:44 pm
Please log in for more thread options
Here is mysterious code, please look:

$x = '12aba34ba5';
@num = split /(a|b)+/, $x;

now, @num has ('12','a','34','a','5').

I don't understand.
I was expecting that @num would have '12','34','5'.
However, it is not.

Why..? Please help me.



Posted by Joost Diepenmaat on March 10, 2008, 5:48 pm
Please log in for more thread options

> Here is mysterious code, please look:
>
> $x = '12aba34ba5';
> @num = split /(a|b)+/, $x;
>
> now, @num has ('12','a','34','a','5').
>
> I don't understand.
> I was expecting that @num would have '12','34','5'.
> However, it is not.

See perldoc -f split:

If the PATTERN contains parentheses, additional list elements
are created from each matching substring in the delimiter.

split(/([,-])/, "1-10,20", 3);

produces the list value

(1, '-', 10, ',', 20)

IOW, you can use some non-capturing syntax, like:

@num = split /[ab]+/,$x;

to discard the separators.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

Posted by xhoster on March 11, 2008, 12:54 pm
Please log in for more thread options
>
> > Here is mysterious code, please look:
> >
> > $x = '12aba34ba5';
> > @num = split /(a|b)+/, $x;
> >
> > now, @num has ('12','a','34','a','5').
> >
> > I don't understand.
> > I was expecting that @num would have '12','34','5'.
> > However, it is not.
>
> See perldoc -f split:
>
> If the PATTERN contains parentheses, additional list elements
> are created from each matching substring in the delimiter.


That really should say "If the PATTERN contains capturing parentheses,..."
^^^^^^^^^

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Posted by Peter Makholm on March 10, 2008, 5:52 pm
Please log in for more thread options

> Why..? Please help me.

Read 'perldoc -f split'.

[...]
If the PATTERN contains parentheses, additional list elements
are created from each matching substring in the delimiter.

split(/([,-])/, "1-10,20", 3);

produces the list value

(1, '-', 10, ',', 20)
[...]

//Makholm

Posted by Riad KACED on March 10, 2008, 9:24 pm
Please log in for more thread options
I would propose the following for your case :
@num = split /D+/,$x;
This will split with any a-zA-Z

Riad.

Similar ThreadsPosted
FAQ 6.4: I put a regular expression into $/ but it didn't work. What's wrong? October 27, 2004, 5:03 pm
FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong? March 2, 2005, 12:03 am
FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong? April 27, 2005, 11:03 pm
FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong? July 13, 2005, 4:03 am
FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong? September 14, 2005, 10:03 pm
FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong? November 5, 2005, 5:03 pm
FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong? November 24, 2005, 11:03 pm
FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong? December 28, 2005, 11:03 am
FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong? May 6, 2006, 9:03 am
FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong? August 24, 2006, 3:03 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap