Reg. string matching using reg-exp

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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:...
    3. 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...
    4. string matching
      I need to remove $b from $a $a = "abc/\def/ghi" ; $b = "abc/\def"; Regards Kamal
    5. 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...
  3. #2

    Default 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

  4. #3

    Default 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

  5. #4

    Default 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: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)
    Do you have the \r\n at the end of $line?
    Wolf

    Wolf Blaum Guest

  6. #5

    Default 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]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

  7. #6

    Default 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: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";
    > }
    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: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

  8. #7

    Default 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



    Rob Dixon Guest

  9. #8

    Default 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(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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139