Click here to get back home

regexp for s///

 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
regexp for s/// Petr Vileta 03-06-2008
Posted by John W. Krahn on March 6, 2008, 3:09 pm
Please log in for more thread options
Petr Vileta wrote:
> I need to write regexp for s/// subtitution but I can't to thinkup it.
> Can anybody help me?
>
> I have string with possible 3 values:
>
> $string = "A: aaa B: bbb";
> or
> $string = "A: aaa";
> or
> $string = "B: bbb";
>
> Now I want to create 2 variables called $myA and $myB and want to store
> values to both variables depend on found or not found A and B in $string.
>
> $string = "A: aaa B: bbb";
> $myA="aaa";
> $myB="bbb";
> or
> $string = "A: aaa";
> $myA='aaa';
> $myB='';
> or
> $string = "B: bbb";
> $myA='';
> $myB='bbb';
>
> Till now I use code bellow, but maybe can be writte as regexp.
>
> if($string =~ m/A:\s+([^B]+)\s+B:\s+(.+)/) { $myA = $1; $myB = $2;}
> elsif($string =~ m/A:\s+(.+)/) { $myA = $1; $myB = '';}
> elseif($string =~ m/B:\s+(.+)/) { $myA = ''; $myB = $1;}
> else {$myA = $myB = '';}

$ perl -le'
for my $string ( "A: aaa B: bbb", "A: aaa", "B: bbb", "B: bbb A: aaa" ) {
my ( $myA, $myB ) = $string =~ /(?=.*A:\s*(\S+))?(?=.*B:\s*(\S+))?/;
print qq[$string = "$string" $myA = "$myA" $myB = "$myB"];
}
'
$string = "A: aaa B: bbb" $myA = "aaa" $myB = "bbb"
$string = "A: aaa" $myA = "aaa" $myB = ""
$string = "B: bbb" $myA = "" $myB = "bbb"
$string = "B: bbb A: aaa" $myA = "aaa" $myB = "bbb"



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Posted by Petr Vileta on March 6, 2008, 7:52 pm
Please log in for more thread options
John W. Krahn wrote:
> $ perl -le'
> for my $string ( "A: aaa B: bbb", "A: aaa", "B: bbb", "B: bbb A: aaa"
> ) { my ( $myA, $myB ) = $string =~
> /(?=.*A:\s*(\S+))?(?=.*B:\s*(\S+))?/; print qq[$string =
> "$string" $myA = "$myA" $myB = "$myB"]; }
> '
> $string = "A: aaa B: bbb" $myA = "aaa" $myB = "bbb"
> $string = "A: aaa" $myA = "aaa" $myB = ""
> $string = "B: bbb" $myA = "" $myB = "bbb"
> $string = "B: bbb A: aaa" $myA = "aaa" $myB = "bbb"
>
>
>
What Perl version you use John? In my Perl 5.6.1 this not work and print
already $myA='' $myB=''

--
Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your
mail from another non-spammer site please.)

Please reply to <petr AT practisoft DOT cz>


Posted by Peter J. Holzer on March 8, 2008, 3:54 pm
Please log in for more thread options
> John W. Krahn wrote:
>> $ perl -le'
>> for my $string ( "A: aaa B: bbb", "A: aaa", "B: bbb", "B: bbb A: aaa"
>> ) { my ( $myA, $myB ) = $string =~
>> /(?=.*A:\s*(\S+))?(?=.*B:\s*(\S+))?/; print qq[$string =
>> "$string" $myA = "$myA" $myB = "$myB"]; }
>> '
>> $string = "A: aaa B: bbb" $myA = "aaa" $myB = "bbb"
>> $string = "A: aaa" $myA = "aaa" $myB = ""
>> $string = "B: bbb" $myA = "" $myB = "bbb"
>> $string = "B: bbb A: aaa" $myA = "aaa" $myB = "bbb"

> What Perl version you use John? In my Perl 5.6.1 this not work and print
> already $myA='' $myB=''

You must have a very peculiar version of perl 5.6.1, which is different
from everybody else's perl 5.6.1. I am beginning to suspect that it
exists only in your head.

        hp


Similar ThreadsPosted
Need RegExp November 15, 2004, 7:32 pm
looking for a better regexp November 25, 2004, 6:35 pm
RegExp Help December 6, 2004, 8:16 pm
RegExp Help December 6, 2004, 3:57 pm
regexp February 23, 2005, 4:29 pm
UTF-8 in regexp with 5.8.1 April 10, 2005, 8:22 pm
Help with regexp. Can you do better September 27, 2005, 5:24 am
regexp May 9, 2006, 9:43 am
Regexp help. June 2, 2006, 9:57 am
regexp January 30, 2007, 6:30 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap