Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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

Replies are listed 'Best First'.
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?


      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?


        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 (Sage) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://572859]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2018-03-22 01:34 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (272 votes). Check out past polls.