Hi, Two things would have caused problem in your script. First, you have not put the value of $y in quotations nor you have made a logical termination to the statement. This statement should read as:

my $y = "set mgmtport speed (10|100) duplex (full|half)";

Second, the expression you want to search contains some regular expression meta-characters, the | symbol, the opening and the closing parenthesis. To make a exact search of these characters, you have to include a '\' before the special characters when the variable is defined (I haven't tested this). Or the smarter way is, when you are searching for the text (with regex meta-characters), you can switch off the regular expression before the search string and switch them on once you have matched the exact string, in this case switch them on before you search for the whitespace character. Your search expression should be

if ($x =~ /\Q$y\E\s+/g) { #Do something here }

\Q and \E are special sequence characters which will switch off all regex meta-characters between them. This is useful in cases when searching strings with regular expression meta-character. Exactly the one which you are encountering!!!

Read Common Metacharacters and Features in the book Mastering Regular Expressions by Jeffrey E. F. Friedl if you wish to learn more on this.


In reply to Re: How Do I Skip Spaces At The End of Words Using A Regex? by rsriram
in thread How Do I Skip Spaces At The End of Words Using A Regex? by phemal

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.