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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |