First: literal parens need to be escaped, or they'll be seen as grouping operators. Your input has literal parentheses in it, so you need to take note of that.

Second: Since you can capture more than one match in a regular expression, you need to pay attention to which match you want to grab. When, in your second pattern match, you set $view to $1, you're nabbing what's in the first parentheses, which in this case is what $target is already set to. You either want to get rid of the parentheses around that first \w+ so it doesn't capture, or use $2 to capture what you want. Of course you need to make sure you're taking account of the first point I made above.

Third: note that the minus sign is not a word character, so given your example and what you say you want to capture, you should be using a slightly more complicated regex than (\w+). You need to match word characters and the minus sign, so the natural choice is a character class : [\w\-] (the - must be escaped because it has special meaning inside a character class).

Fourth: you're printing both target and view outside of your conditional. That's not likely to lead to sensible output, since those things are only set if your regular expressions match.

Fifth, and I'll leave this as an exercise for you, there's a more efficient way to do this than match things twice. Use the power of list assignment with your regex matches and capturing parentheses, here's a sample of how that works:

$_ ="I have a lovely bunch of bananas"; if ( my ($verb, $adjective, $fruit) = /^I (\w+) a (\w+) \w+ of (\w+)/ +) { print "It would seem your $fruit, which you describe as '$adjectiv +e', is something you $verb.\n"; }

The inner block (the "if") here is only executed if the pattern matches, so if you put something like that inside your loop, it will be quite efficient, since you only try to match once instead of twice.

HTH


In reply to Re: regex on a line by arturo
in thread regex on a line by Sara

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.