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 Frank Seitz on March 7, 2008, 12:12 am
Please log in for more thread options
Petr Vileta wrote:
> Frank Seitz wrote:
>>
>>my ($myA) = $string =~ /A:\s+(\S+)/;
>>$myA //= '';
>>my ($myB) = $string =~ /B:\s+(\S+)/;
>>$myB //= '';
>>
>>(//= Perl 5.10)
>
> Thank you Frank for response, but your solution is unusable for now. On many
> servers Perl 5.8 is installed and on some 5.6.1 too.

$myA //= ''

is equivalent to

$myA = '' if !defined $myA;

Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel

Posted by Gunnar Hjalmarsson on March 7, 2008, 4:25 am
Please log in for more thread options
Frank Seitz wrote:
> Petr Vileta wrote:
>> Frank Seitz wrote:
>>>
>>> my ($myA) = $string =~ /A:\s+(\S+)/;
>>> $myA //= '';
>>> my ($myB) = $string =~ /B:\s+(\S+)/;
>>> $myB //= '';
>>>
>>> (//= Perl 5.10)
>>
>> Thank you Frank for response, but your solution is unusable for now. On many
>> servers Perl 5.8 is installed and on some 5.6.1 too.
>
> $myA //= ''
>
> is equivalent to
>
> $myA = '' if !defined $myA;

$myA ||= '';

works just as well, doesn't it?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Posted by Frank Seitz on March 7, 2008, 4:33 am
Please log in for more thread options
Gunnar Hjalmarsson wrote:
> Frank Seitz wrote:
>>
>>$myA //= ''
>>
>>is equivalent to
>>
>>$myA = '' if !defined $myA;
>
> $myA ||= '';
>
> works just as well, doesn't it?

$myA = 0;
$myA ||= ''; # ups

Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel

Posted by Gunnar Hjalmarsson on March 7, 2008, 6:28 am
Please log in for more thread options
Frank Seitz wrote:
> Gunnar Hjalmarsson wrote:
>> Frank Seitz wrote:
>>> $myA //= ''
>>>
>>> is equivalent to
>>>
>>> $myA = '' if !defined $myA;
>>
>> $myA ||= '';
>>
>> works just as well, doesn't it?
>
> $myA = 0;
> $myA ||= ''; # ups

Sure, but somehow I got the impression that the possible value (the OP
gave us the example 'aaa') couldn't be 0. Only the OP can tell.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Posted by Abigail on March 6, 2008, 2:02 pm
Please log in for more thread options
_
Petr Vileta (stoupa@practisoft.cz) wrote on VCCCI September MCMXCIII in
][ I need to write regexp for s/// subtitution but I can't to thinkup it. Can
][ anybody help me?

Well, yeah. It seems you want to put something in $myA and $myB depending
on the content of $sting, without changing the latter. Why bother with s///?

][ I have string with possible 3 values:

Then why bother with a regexp?

][ $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.

The code below *USES* regexes. So, keep it if you want to have it using
a 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 = '';}


But I wouldn't use a regexp. I'd write (untested):

give ($string) {
when ("A: aaa B: bbb") {
$myA = "aaa";
$myB = "bbb";
}
when ("A: aaa") {
$myA = "aaa";
$myB = "";
}
when ("B: bbb") {
$myA = "";
$myB = "bbb";
}
}


And if I were to insist on using a regexp, I'd use (untested):

if ($string =~ /^(?:A: (?<a>aaa))? ?(?:B: (?<b>bbb))?$/) {
$myA = $+ // "";
$myB = $+ // "";
}

But I find the given/when much clearer.


Abigail
--
#!/opt/perl/bin/perl -w
$\ = $"; $; = $$; END {$: and print $:} $SIG = sub {$ := $_}; kill 15 =>
fork and ($; == getppid and exit or wait) foreach qw /Just another Perl Hacker/

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