Split String Based On Regex In Perl

liunx

Guest
Let's assume this is what I'm working with...<br /><br />$string = 'xxxxxxxxyyyy';<br />$regex = 'xxxxxxxx';<br /><br />And my goal is to have the following results...<br /><br />$value1 = 'xxxxxxxx';<br />$value2 = 'yyyy'; (Remainder after $regex)<br /><br />What commands am I using to get to that point?<br />I have used regex to remove and/or alter text, but<br />I have never used them to split apart a string.<br /><br />I can hack around to get a result, but I prefer to<br />do it as 'properly' as possible and minimizing lines<br />and execution cycles.<!--content-->
I believe preg_split() may be what you want. Check out: <a href="http://www.php.net/manual/en/function.preg-split.php" target="_blank">http://www.php.net/manual/en/function.preg-split.php</a><br /><br />On second thought, it doesn't look like there's anything separating the two parts of your string so it may not be exactly what you're looking for.<!--content-->
OK, looks like this should give you what you want.... I think <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->$string = 'xxxxxxxxyyyy';<br />$regex = '/(xxxxxxxx)/';<br /><br />$result = preg_split( $regex, $string, 2, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE );<!--c2--></div><!--ec2--><!--content-->
<!--quoteo(post=205884:date=May 10 2007, 05:52 PM:name=click)--><div class='quotetop'>QUOTE(click @ May 10 2007, 05:52 PM) <a href="http://www.totalchoicehosting.com/forums/index.php?act=findpost&pid=205884"><img src='http://www.totalchoicehosting.com/forums/style_images/1/post_snapback.gif' alt='*' border='0' /></a></div><div class='quotemain'><!--quotec-->$result = preg_split( $regex, $string, 2, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE );[/code]<!--QuoteEnd--></div><!--QuoteEEnd--><br /><br />Except preg_split is a php function and he's using perl.<br /><br />This is the way I'd probably do it, although there may be a better way (made up the regex to match on example string just to show):<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->$string = 'xxxxxxxxyyyy';<br />$regex = '(x)+';<br /><br />if ($string =~ m/$regex/) {<br />   $value1 = $&;<br /><br />   $value2 = $string;<br />   $value2 =~ s/$regex//;<br />}<!--c2--></div><!--ec2--><br /><br />Result of that:<br />$value1 = 'xxxxxxxx'<br />$value2 = 'yyyy'<br /><br />I'm not aware of a way to split the string up in a single operation within perl natively.<!--content-->
Thanks for the idea, guys. I found the solution.<br /><br />my($v1, $v2) = $string =~ m|($regex)(.+)|;<br /><br />I have input the syntax in my script and it works great.<br />Man, I love Perl. There's never an end to the features.<!--content-->
nice <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/thumbup1.gif" style="vertical-align:middle" emoid=":thumbup1:" border="0" alt="thumbup1.gif" />.<!--content-->
<!--quoteo(post=205895:date=May 10 2007, 11:53 PM:name=TCH-MikeJ)--><div class='quotetop'>QUOTE(TCH-MikeJ @ May 10 2007, 11:53 PM) <a href="http://www.totalchoicehosting.com/forums/index.php?act=findpost&pid=205895"><img src='http://www.totalchoicehosting.com/forums/style_images/1/post_snapback.gif' alt='*' border='0' /></a></div><div class='quotemain'><!--quotec-->Except preg_split is a php function and he's using perl.<!--QuoteEnd--></div><!--QuoteEEnd--><br />hahaha I think that officially makes me a moron! <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/oops.gif" style="vertical-align:middle" emoid=":oops:" border="0" alt="oops.gif" /> <br /><br />Sorry, I don't know how I managed to miss that. I guess the $ variables threw me into php mode (I haven't done any significant perl in a while). Maybe there should be some basic IQ test before we're allowed to post. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/biggrin.gif" style="vertical-align:middle" emoid=":D" border="0" alt="biggrin.gif" /> I think the Perl split() works pretty much the same as preg_split(), though. Any part of the regex in parenthesis will be included in the result array.<br /><br />Anyhow, I'm glad you figured it out.<!--content-->
 
Back
Top