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:
Matching help needed

 



yim11
Novice

Oct 17, 2000, 8:00 AM

Post #1 of 6 (608 views)
Matching help needed Can't Post

Hello,
I have some code that pulls certian information from the subject line of a email.

the code is as follows:

$catno="";
for each(@tmp) {
if ($_=~m"[0-9]") {
$catno=$_;
}
}
---------
this code grabs the catno. (in the form ###-####) from the message subject. The problem is the way is written now it will also grab any other numbers it finds with a '-' character. For example, if the subject is "Re: 000-0000 and 123455-123455" will result in the 123455-123455 being extracted, not the 000-0000.
I need to alter this code to only match the numbers found immeditately before and after the first "-" found in the string, and ignore any other '-' characters in the string.

Any and all help on this is greatly appreciated!
TIA~
Jim


perlkid
stranger

Oct 16, 2000, 10:19 PM

Post #2 of 6 (608 views)
Re: Matching help needed [In reply to] Can't Post

 
Oh Ok,

Then use this,

$input = "Re: 000-0000 and 123455-123455";
$input =~ s!(\s\d+)-(\d+)\s!$1-$2!;
$wanted = "$1-$2";

print "$wanted"; ## If you want to test it.

perlkid


perlkid
stranger

Oct 17, 2000, 9:04 AM

Post #3 of 6 (608 views)
Re: Matching help needed [In reply to] Can't Post

 
So you need to take out all the numbers preceding the - ?

If So I think that this will work.

$input_field =~ s!^(\d)-(\d)\s!!;

That will turn

"Re: 000-0000 and 123455-123455"

into

"Re: and 123455-123455"

If your talking about getting

"Re: 000-0000 and 123455-123455"

Into

"Re: 00000 and 123455-123455"

Then use this

$input_field =~ s!(\d)-(\d)!!;

I didn't know what exactly you meant my imediatly before and after. Maybe that's what you want.

perlkid


yim11
Novice

Oct 17, 2000, 9:08 AM

Post #4 of 6 (608 views)
Re: Matching help needed [In reply to] Can't Post

Thank you for your reply.
Sorry I was kinda vague on what I needed.

What I want to do is turn
"Re: 000-0000 and 123455-123455"
into
"000-0000"
and ignore everything else.

Thanks for your help,
Jim


yim11
Novice

Oct 17, 2000, 11:05 AM

Post #5 of 6 (608 views)
Re: Matching help needed [In reply to] Can't Post

Thank you very much for your help.

As I was awaiting your reply, I was reading about minimal matching and was wondering if I could have used '??' to match the '-' character zero or 1 time? I could not find many examples for this though.

Thanks!
Jim


perlkid
stranger

Oct 17, 2000, 11:18 AM

Post #6 of 6 (608 views)
Re: Matching help needed [In reply to] Can't Post

 
Glad You Got It Working.

I love regular expressions now. I learned everything I can about them from this book. Now I like to try and make new regex up that I wouldn't normally use to test myself , see how much I can do with what I know about them. It's really fun.

perlkid

 
 


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

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