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 Petr Vileta on March 6, 2008, 12:30 pm
Please log in for more thread options
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 = '';}


--
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 Glenn Jackman on March 6, 2008, 1:01 pm
Please log in for more thread options
At 2008-03-06 12:30PM, "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.

How about a hash?

my %my = $string =~ /([AB]): (\w+)/g;

That will be problematic depending what's on the right side of the
colon.


--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry

Posted by Frank Seitz on March 6, 2008, 1:23 pm
Please log in for more thread options
Petr Vileta wrote:
>
> 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 = '';}

my ($myA) = $string =~ /A: (aaa)/;
$myA //= '';
my ($myB) = $string =~ /B: (bbb)/;
$myB //= '';

(//= Perl 5.10)

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 Frank Seitz on March 6, 2008, 1:27 pm
Please log in for more thread options
Petr Vileta wrote:
>
> 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 = '';}

my ($myA) = $string =~ /A:\s+(\S+)/;
$myA //= '';
my ($myB) = $string =~ /B:\s+(\S+)/;
$myB //= '';

(//= Perl 5.10)

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 Petr Vileta on March 6, 2008, 7:32 pm
Please log in for more thread options
Frank Seitz wrote:
> Petr Vileta wrote:
>>
>> 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 = '';}
>
> 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.

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


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