CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Typical Search and repalce with Regular expression

 



hydpm
User

Sep 5, 2008, 12:46 PM

Post #1 of 4 (3163 views)
Typical Search and repalce with Regular expression Can't Post

Hi,

I wanted to do the following.

$Input may have one of the following values:

10 m

1 h

2 h

3 d

4 w



What i need to do is :

I should grab the one of the above values from $input then search and repalce like below:

10 min/s

1 hour

2 hours

3 day/s

4 weeks



Where i blocked is :

(\S+) mathces other than spaces i.e m,h,h/s,d,w. The conversion will be if "1 h" found then it should be converted to "1 hour" . If "2 h" then it should be converted to "2 hours". If it is "10 m" then to "10 min/s" etc. How do i make the expression conditionally here. Need help on that?

The more tricky on hour part.

$input=~s/(\d+)(\s+)(\S+)/$1$2(Need Help here)/



Thanks,

Raj


KevinR
Veteran


Sep 5, 2008, 1:00 PM

Post #2 of 4 (3161 views)
Re: [wingsof5r] Typical Search and repalce with Regular expression [In reply to] Can't Post

Probably the simplest is just use a hash to store the pattern replacement strings and then something like this:


Code
my %conversion = ( 
m => 'min',
h => 'hour',
d => 'day',
w => 'week'
);

while (<DATA>) {
chomp;
/^(\d+)(\s+)(\S+)$/;
my $symbol = $conversion{lc($3)};
if ($1 > 1) {
$symbol .= 's';
}
print "$1$2$symbol\n";
}
__DATA__
10 m
1 h
2 h
3 d
4 w

-------------------------------------------------


hydpm
User

Sep 5, 2008, 1:05 PM

Post #3 of 4 (3159 views)
Re: [KevinR] Typical Search and repalce with Regular expression [In reply to] Can't Post

Kevin,

You are so spontaneous. While typing a post i am thinking of you. i thought you might the first one gonna reply.

Thanks for making my guess true. I wish to meet you one day.....:)



Thank you very much once again.....

Raj


KevinR
Veteran


Sep 5, 2008, 1:11 PM

Post #4 of 4 (3155 views)
Re: [wingsof5r] Typical Search and repalce with Regular expression [In reply to] Can't Post

You're welcome. Cool
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives