For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2006 > Regular Expression Help









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Regular Expression Help
Ashish Srivastava

2006-11-01, 7:58 am

I have a string which have multiple placeholder, for example:

$str = 'Hello [[Name]], Your login id is [[Login]] !!!";

I want to replace all placeholder with some identifier. In above example: identifier are Name and Login.
The regular expression for this requirment is pretty staightward:

$str =~ s/\[\[([\d\w\-]+)\]\]/$1/g;

Now the problem is there could be Nested Placeholders. For example:

$str = 'Error is: [[[[id]]-[[message]]]]'

so in above example if idenfiers are defined as:

id = 1001
Message = "Crash"
1001-Crash = "System overloaded"

so output of my required regular expression for subsitution shouldbe:

Error is: System overloaded

Any response on this issue will be very helpful.

Thank You.
Ashish Srivastava



________________________________________
__________________
Yahoo! India Answers: Share what you know. Learn something new
http://in.answers.yahoo.com/
John W. Krahn

2006-11-01, 7:58 am

Ashish Srivastava wrote:
> I have a string which have multiple placeholder, for example:
>
> $str = 'Hello [[Name]], Your login id is [[Login]] !!!";
>
> I want to replace all placeholder with some identifier. In above example: identifier are Name and Login.
> The regular expression for this requirment is pretty staightward:
>
> $str =~ s/\[\[([\d\w\-]+)\]\]/$1/g;
>
> Now the problem is there could be Nested Placeholders. For example:
>
> $str = 'Error is: [[[[id]]-[[message]]]]'
>
> so in above example if idenfiers are defined as:
>
> id = 1001
> Message = "Crash"
> 1001-Crash = "System overloaded"
>
> so output of my required regular expression for subsitution should be:
>
> Error is: System overloaded


$ perl -le'
my %t = (
id => 1001,
message => "Crash",
"1001-Crash" => "System overloaded",
);

my $str = q/Error is: [[[[id]]-[[message]]]]/;

print $str;

1 while $str =~ s/\[\[([\w-]+)]]/$t{$1}/g;

print $str;
'
Error is: [[[[id]]-[[message]]]]
Error is: System overloaded




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

2006-11-01, 6:57 pm

Ashish ,
you might have the [ in a recurring sequence using [*

On 11/1/06, John W. Krahn <krahnj@telus.net> wrote:
>
> Ashish Srivastava wrote:
> example: identifier are Name and Login.
>
> $ perl -le'
> my %t = (
> id => 1001,
> message => "Crash",
> "1001-Crash" => "System overloaded",
> );
>
> my $str = q/Error is: [[[[id]]-[[message]]]]/;
>
> print $str;
>
> 1 while $str =~ s/\[\[([\w-]+)]]/$t{$1}/g;
>
> print $str;
> '
> Error is: [[[[id]]-[[message]]]]
> Error is: System overloaded
>
>
>
>
> 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
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>


Ashish Srivastava

2006-11-01, 9:56 pm

Thank You John and Anshul for response. It was great help.

Regards,
Ashish Srivastava





----- Original Message ----
From:Anshul Saxena <anshulacxiom@gmail.com>
To: John W. Krahn <krahnj@telus.net>
Cc: Perl Beginners <beginners@perl.org>
Sent: Wednesday, 1 November, 2006 11:01:38 PM
Subject: Re: Regular Expression Help


Ashish ,
you might have the [ in a recurring sequence using [*

On 11/1/06, John W. Krahn <krahnj@telus.net> wrote:
>
> Ashish Srivastava wrote:
> example: identifier are Name and Login.
>
> $ perl -le'
> my %t = (
> id => 1001,
> message => "Crash",
> "1001-Crash" => "System overloaded",
> );
>
> my $str =q/Error is: [[[[id]]-[[message]]]]/;
>
> print $str;
>
> 1 while $str =~ s/\[\[([\w-]+)]]/$t{$1}/g;
>
> print $str;
> '
> Error is:[[[[id]]-[[message]]]]
> Error is: System overloaded
>
>
>
>
>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
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>




________________________________________
__________________
Yahoo! India Answers: Share what you know. Learn something new
http://in.answers.yahoo.com/
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com