RE: Reg. string matching using reg-exp
It looks like there is no '\r\n' at the end of $line. -Original Message- From: Balaji Thoguluva [mailto:[EMAIL PROTECTED] Sent: Friday, February 06, 2004 11:45 AM To: [EMAIL PROTECTED] Subject: Reg. string matching using reg-exp #!/usr/bin/perl -w my $line= 'INVITE sip:[EMAIL PROTECTED] SIP/2.0'; if($line =~ /^\s*(\w+)\s*(.*)\s+(sip\/\d+\.\d+)\s*\r\n/i) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Reg. string matching using reg-exp
For Quality purpouses, Balaji Thoguluva 's mail on Friday 06 February 2004 20:44 may have been monitored or recorded as: Hi, I am a novice to perl programming. When I execute the following code, I get always No Match. I guess my reg-exp is correct. I also tried changing $line= INVITE sip:[EMAIL PROTECTED] SIP/2.0; to have double quotes and a backslash char before @ symbol. Even then it gives me No Match. I appreciate your help. #!/usr/bin/perl -w my $line= 'INVITE sip:[EMAIL PROTECTED] SIP/2.0'; if($line =~ /^\s*(\w+)\s*(.*)\s+(sip\/\d+\.\d+)\s*\r\n/i) Do you have the \r\n at the end of $line? Wolf -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Reg. string matching using reg-exp
Use the 's' option at the end of your regex after the closing '/'. $var =~ /match\nsomething\nelse/s; read 'perldoc perlre' for more about regexes. Also, please reply to the list next time, because I might not be at my desk or able to reply, and someone else probably will. _ From: Balaji Thoguluva [mailto:[EMAIL PROTECTED] Sent: Friday, February 06, 2004 12:07 PM To: Tim Johnson Subject: RE: Reg. string matching using reg-exp Thanks Tim Johnson. I removed the /r/n from the reg-ex and it works. I have another question. How to assign a multiline string or string having many lines(strings having \n) to a $string-variable?. In C, there is a \ operator. Thanks for your help, Balaji
RE: Reg. string matching using reg-exp
On Feb 6, Balaji Thoguluva said: Thanks Tim Johnson. I removed the /r/n from the reg-ex and it works. I have another question. How to assign a multiline string or string having many lines(strings having \n) to a $string-variable?. In C, there is a \ operator. You don't need to do anything special in Perl. $string = This is a very long string that spans many lines; Or you can use a 'here-doc'. $string = END OF STRING; this is a very long string that spans many lines END OF STRING -- Jeff japhy Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ stu what does y/// stand for? tenderpuss why, yansliterate of course. [ I'm looking for programming work. If you like my work, let me know. ] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Reg. string matching using reg-exp
Balaji thoguluva wrote: I am a novice to perl programming. When I execute the following code, I get always No Match. I guess my reg-exp is correct. I also tried changing $line= INVITE sip:[EMAIL PROTECTED] SIP/2.0; to have double quotes and a backslash char before @ symbol. Even then it gives me No Match. I appreciate your help. #!/usr/bin/perl -w my $line= 'INVITE sip:[EMAIL PROTECTED] SIP/2.0'; if($line =~ /^\s*(\w+)\s*(.*)\s+(sip\/\d+\.\d+)\s*\r\n/i) { print \nSIP Method: ,$1; print \nRequest URI: ,$2; print \nSIP Version: ,$3; } else { print No Match; } The string you show, as others have pointed out, has no \r\n terminator. Even if you're pulling your record from a text file, Perl will try hard to change all platforms' line terminators to a simple \n in the record you actually read. And even having said that, there's no reason to match /all/ of the string if you're just extracting sub-fields: it's unlikely to help to be told that your record doesn't actually end in /\s*\r\n/. I think your regex should look like this: my $line= 'INVITE sip:[EMAIL PROTECTED] SIP/2.0'; $line =~ /^\s*(\w+)\s+(.*)\s+(sip\/\d+\.\d+)/i; print map $_\n, $1, $2, $3; **OUTPUT INVITE sip:[EMAIL PROTECTED] SIP/2.0 HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Reg. string matching using reg-exp
Jeff 'Japhy' Pinyan wrote: On Feb 6, Balaji Thoguluva said: Thanks Tim Johnson. I removed the /r/n from the reg-ex and it works. I have another question. How to assign a multiline string or string having many lines(strings having \n) to a $string-variable?. In C, there is a \ operator. You don't need to do anything special in Perl. $string = This is a very long string that spans many lines; Or you can use a 'here-doc'. $string = END OF STRING; this is a very long string that spans many lines END OF STRING In C, newlines have to be introduced explicitly as \n. A literal newline character (the end of a source record) has to be escaped to make it 'vanish', otherwise it should throw a compilation error. In Perl: my $string = One Two Three ; In C: char *string = One\n\ Two\n\ Three\n\ ; or, because consecutive C string constants are implicitly concatenated: char *string = One\n Two\n Three\n ; HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Reg. string matching using reg-exp
Rob Dixon wrote: In C, newlines have to be introduced explicitly as \n. A literal newline character (the end of a source record) has to be escaped to make it 'vanish', otherwise it should throw a compilation error. In Perl: my $string = One Two Three ; In C: char *string = One\n\ Two\n\ Three\n\ ; or, because consecutive C string constants are implicitly concatenated: char *string = One\n Two\n Three\n ; or don't quote them is you have an ANSI C compiler: #define TEST(s) printf(%s\n,#s) int main(int argc,char* argv[]){ TEST(\n \n \n see you!); } prints: see you! david -- sub'_{print@_ ;* \ = * __ ,\ \} sub'__{print@_ ;* \ = * ___ ,\ \} sub'___{print@_ ;* \ = * ,\ \} sub'{print@_,\n}{_+Just}(another)-(Perl)-(Hacker) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response