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: Intermediate:
Simple regular expression

 



martsbradley
Deleted

Dec 6, 2000, 8:33 AM

Post #1 of 6 (418 views)
Simple regular expression Can't Post

How can I strip the number off this string. The number always appears at the end of the string.

$mystring = "count99"

#I want the string to become = 99
thanks.

------------------
Martin


japhy
Enthusiast

Dec 6, 2000, 8:42 AM

Post #2 of 6 (418 views)
Re: Simple regular expression [In reply to] Can't Post

There are many ways to approach it, but I'd use:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


($string) = $string =~ /(\d+)$/;
</pre><HR></BLOCKQUOTE>

------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher



BikerHQ
Deleted

Dec 6, 2000, 8:52 AM

Post #3 of 6 (418 views)
Re: Simple regular expression [In reply to] Can't Post

Hey Japhy,

Don't suppose you wanna explain what that means! :)

I have just started in perl, but the /(\d+)$/ part of that string just confuses me. What is it doing?


sleuth
Enthusiast / Moderator

Dec 6, 2000, 9:49 AM

Post #4 of 6 (418 views)
Re: Simple regular expression [In reply to] Can't Post

 
No Problem Japhy, I'll help out :),

\d means all numeric characters,

$ means to the end of the string

Putting the \d in () can be used to save everything that the \d applies to and store it into $1. Like so,

If you want to find all the numbers in a string and put say ... brackets around them, this will do it,

$string =~ s/(\d+)/[$1]/;

Now as for that +, it is a qualifier, and it tells \d how far it should go, \d by itself will only apply to the first number it finds, but \d+ will apply to the first group of numbers it finds, because the + means 1 or more.

When you put the $ after the (\d+) it tells the \d to grab the group of numbers at the end of the string.

Does that clear it up?

Sleuth


BikerHQ
Deleted

Dec 6, 2000, 11:00 AM

Post #5 of 6 (418 views)
Re: Simple regular expression [In reply to] Can't Post

I think I'll just stop using perl right now! :) Good explanation though!

(I should have stuck to programming the Sinclair Spectrum....)


martsbradley
Deleted

Dec 6, 2000, 11:05 AM

Post #6 of 6 (418 views)
Re: Simple regular expression [In reply to] Can't Post

Thanks, this is a great help.

Martin Bradley

 
 


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

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