Ask a Question related to PERL Beginners, Design and Development.
-
Balaji thoguluva #1
Reg. string matching using reg-exp
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:3311\@192.168.6.100 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:3311@192.168.6.100 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";
}
Thanks,
Balaji
Yahoo! India Mobile: Ringtones, Wallpapers, Picture Messages and more.Download now.
Balaji thoguluva Guest
-
Aprroximate string matching
I am writing a script to check my newly ripped MP3 music in an INBOX to the stuff that I arleady have in the MUSIC directory. What I need is some... -
Pattern matching for xx-xx-xx string
Hello, What is the syntax to make sure the user has entered this pattern in a web form: xx-xx-xx where x are integers, for example:... -
Matching String
Hi again! Im saving some data of a configuration file in scalar var. My configuration file has something like this: mailaddr=mail1@yes.com... -
string matching
I need to remove $b from $a $a = "abc/\def/ghi" ; $b = "abc/\def"; Regards Kamal -
String matching not working
Hi, I am quite new to Perl. I have several sets of data and within each individual file news stories are seperated using ========. My goal is... -
Tim Johnson #2
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:bbthog2@yahoo.co.in]
Sent: Friday, February 06, 2004 11:45 AM
To: [email]beginners@perl.org[/email]
Subject: Reg. string matching using reg-exp
#!/usr/bin/perl -w
my $line= 'INVITE sip:3311@192.168.6.100 SIP/2.0'; if($line =~
/^\s*(\w+)\s*(.*)\s+(sip\/\d+\.\d+)\s*\r\n/i)
Tim Johnson Guest
-
Tim Johnson #3
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:bbthog2@yahoo.co.in]
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
Tim Johnson Guest
-
Wolf Blaum #4
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:Do you have the \r\n at the end of $line?> 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:3311\@192.168.6.100 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:3311@192.168.6.100 SIP/2.0';
> if($line =~ /^\s*(\w+)\s*(.*)\s+(sip\/\d+\.\d+)\s*\r\n/i)
Wolf
Wolf Blaum Guest
-
Jeff 'Japhy' Pinyan #5
RE: Reg. string matching using reg-exp
On Feb 6, Balaji Thoguluva said:
You don't need to do anything special in Perl.>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.
$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]japhy@pobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
<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. ]
Jeff 'Japhy' Pinyan Guest
-
Rob Dixon #6
Re: Reg. string matching using reg-exp
Balaji thoguluva wrote:
I also tried changing $line= "INVITE sip:3311\@192.168.6.100 SIP/2.0"; to have double quotes and a backslash char before @ symbol.> 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.
Even then it gives me No Match. I appreciate your help.The string you show, as others have pointed out, has no "\r\n" terminator.>
> #!/usr/bin/perl -w
> my $line= 'INVITE sip:3311@192.168.6.100 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";
> }
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:3311@192.168.6.100 SIP/2.0';
$line =~ /^\s*(\w+)\s+(.*)\s+(sip\/\d+\.\d+)/i;
print map "$_\n", $1, $2, $3;
**OUTPUT
INVITE
sip:3311@192.168.6.100
SIP/2.0
HTH,
Rob
Rob Dixon Guest
-
Rob Dixon #7
Re: Reg. string matching using reg-exp
Jeff 'Japhy' Pinyan wrote:
In C, newlines have to be introduced explicitly as "\n". A literal>
> 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
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
Rob Dixon Guest
-
David #8
Re: Reg. string matching using reg-exp
Rob Dixon wrote:
or don't quote them is you have an ANSI C compiler:> 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"
> ;
#define TEST(s) printf("%s\n",#s)
int main(int argc,char* argv[]){
TEST(xxxx\n
yyyy\n
zzzz\n
see you!);
}
prints:
xxxx
yyyy
zzzz
see you!
david
--
sub'_{print"@_ ";* \ = * __ ,\ & \}
sub'__{print"@_ ";* \ = * ___ ,\ & \}
sub'___{print"@_ ";* \ = * ____ ,\ & \}
sub'____{print"@_,\n"}&{_+Just}(another)->(Perl)->(Hacker)
David Guest



Reply With Quote

