Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

What is regular expression for the following thing?

by sanjay nayak (Sexton)
on Oct 20, 2008 at 08:58 UTC ( [id://718166]=perlquestion: print w/replies, xml ) Need Help??

sanjay nayak has asked for the wisdom of the Perl Monks concerning the following question:


Hi Monks

Plz suggest me a suitable Regular expression for the following conditions.

REGISTER or INVITE or BYE (space) some text or digit or both -(hypen) or @ or _(underscore sign) some text or digit or both . some text or digit or both space SIP/2.0.

For example

my $text="REGISTER acti-abc.test SIP/2.0";

If ($text ~= /^REGISTER|INVITE|BYE\s\w\.\w\sSIP\/2.0/)

The above is not working properly, Plz suggest me a suitable regular expression for the above.

Regd's
Sanjay
  • Comment on What is regular expression for the following thing?

Replies are listed 'Best First'.
Re: What is regular expression for the following thing?
by ikegami (Patriarch) on Oct 20, 2008 at 09:07 UTC

    /^REGISTER|INVITE|BYE\s\w\.\w\sSIP\/2.0/
    means
    ^REGISTER or INVITE or BYE\s\w\.\w\sSIP\/2.0
    You need to limit the extent of the alternation.
    /^(?:REGISTER|INVITE|BYE)\s\w\.\w\sSIP\/2.0/

    There's also the issue that the last "." should be "\.".

    There's also the issue that the "\w" doesn't match what you want it to match. You'll have to use a character class ([...]) instead.

Re: What is regular expression for the following thing?
by Corion (Patriarch) on Oct 20, 2008 at 09:08 UTC

    You have an error with precedence in your regular expression. The alternation (|) doesn't know where you want it to stop. See YAPE::Regex::Explain if you want a module that explains regular expressions to you.

    Your regular expression currently matches like

    (?-imsx:^REGISTER|INVITE|BYE\s\w\.\w\sSIP/2.0) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- REGISTER 'REGISTER' ---------------------------------------------------------------------- | OR ---------------------------------------------------------------------- INVITE 'INVITE' ---------------------------------------------------------------------- | OR ---------------------------------------------------------------------- BYE 'BYE' ---------------------------------------------------------------------- \s whitespace (\n, \r, \t, \f, and " ") ---------------------------------------------------------------------- \w word characters (a-z, A-Z, 0-9, _) ---------------------------------------------------------------------- \. '.' ---------------------------------------------------------------------- \w word characters (a-z, A-Z, 0-9, _) ---------------------------------------------------------------------- \s whitespace (\n, \r, \t, \f, and " ") ---------------------------------------------------------------------- SIP/2 'SIP/2' ---------------------------------------------------------------------- . any character except \n ---------------------------------------------------------------------- 0 '0' ----------------------------------------------------------------------

    Maybe you want the Net::SIP module instead of writing your own module?

    As an aside, putting your code between <code>...</code> tags makes the code render and download nicely for those who want to try it.

    Update: YAPE::Regex::Explain installed and I updated the explanation

Re: What is regular expression for the following thing?
by pysome (Scribe) on Oct 20, 2008 at 09:40 UTC
    ~= is not a Perl Match operation.Pls replace it with "=~"
Re: What is regular expression for the following thing?
by Punitha (Priest) on Oct 20, 2008 at 09:13 UTC

    Hi sanjay nayak

    You can have a quick reference about the perl regular expressions here http://perldoc.perl.org/perlrequick.html

    Moreover in your example coding you misused the matching operator. It should be =~ and you may want something like this

    if ($text =~ /^(?:REGISTER|INVITE|BYE)\s\w+[-@_]\w+\.\w+\sSIP\/2\.0/) { print "IN:$text\n"; }

    Punitha

      I think you meant [-\@_].
      >perl -le"@_='abc'; print qr/[-@_]/;" (?-xism:[-abc])
Re: What is regular expression for the following thing?
by luckypower (Beadle) on Oct 20, 2008 at 09:13 UTC
    hey sanjay ,
    use this one..
     if ($text =~ /^(REGISTER|INVITE|BYE)\s\w+(-|@|_)\w+\.\w+\sSIP\/2\.0$/)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://718166]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-19 12:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found