Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

pattern matching for string [SYN, ACK]

by phemal (Sexton)
on Sep 14, 2006 at 04:34 UTC ( #572859=perlquestion: print w/ replies, xml ) Need Help??
phemal has asked for the wisdom of the Perl Monks concerning the following question:

Suppose i have string containing both "[SYN]" and "[SYN, ACK]" patterns. can someone help me writing a patern matching for the above.

i tried /\bstring\b/ but both pattern have square brackets and hence not able to find a suitable match.

Code tags added by GrandFather

Comment on pattern matching for string [SYN, ACK]
Select or Download Code
Re: pattern matching for string [SYN, ACK]
by davido (Archbishop) on Sep 14, 2006 at 04:39 UTC

    Are you looking for this?

    m/\[SYN(?:, ACK)?\]/

    What are you actually trying to accomplish?


    Dave

      the end user may pass any string for ex: $x = "/[SYN, ACK/]" or [SYN] or [SYN, FIN] or [SYN, RST]

      i am looking for a pattern of the above

      Code tags added by GrandFather

        So the specification changed? It seems that the rules for what you would like to match have expanded since your original post. Are you trying to match to capture? Or are you trying to match to reject strings that don't fit? I repeat, what are you trying to accomplish. What is it that you're doing; the bigger picture. It may be clear to you, but it's still quite vague to us.

        A pattern that matches all of the above could be:

        m/\[SYN(?:, (?:ACK|FIN|RST))?\]/

        There's an accurate answer to your question, as asked. Do you now care to elaborate so that our answers can be a little more specific, or have we answered your question satisfactorily?


        Dave

        I'm still not clear on what you want.

        Maybe you want:

        $user_string = "[SYN, ACK]"; if ($x =~ /\Q$user_string\E/) { print("match\n"); } else { print("no match\n"); }

        Maybe you want:

        $user_string = "[SYN, ACK]"; if ($x =~ /(\[.*?\])/) { if ($1 eq $user_string) { print("match\n"); } else { print("no match\n"); } } else { print("not even close\n"); }

        By the way, use &#91; for [ and &#93; for ] in your posts. Better yet, don't escape anything and place code and code bits within <c>...</c> tags.

Re: pattern matching for string [SYN, ACK]
by GrandFather (Cardinal) on Sep 14, 2006 at 04:39 UTC

    Which of the following strings ought match:

    [SYN] [syn] [Syn] [SYN, ACK] [SYN] [SYN, ACK] [ACK] SYN SYN, ACK ACK

    What pattern did you try specifically? The following pattern may be what you want:

    /\[SYN(?:, ACK)?\]/

    Update: couple more test strings added


    DWIM is Perl's answer to Gödel

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (12)
As of 2014-12-26 15:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls