phemal has asked for the wisdom of the Perl Monks concerning the following question:

i want to match a pattern ignoring the white spaces at the end of each word. can anyone help ?

suppose the string is

my $x = "set manager logport [<0-10000>] set manager secondary ip A.B.C.D set mgmtport auto set mgmtport speed (10|100) duplex (full|half) set portsettletime <0-300> set sensor gateway A.B.C.D set sensor ip A.B.C.D E.F.G.H";
now i want to do a pattern match for
my $y = set mgmtport speed (10|100) duplex (full|half) if ($x=~ /$y\s+/ig) { }
if i do the above this thing is not working

2006-09-18 Retitled by holli, as per Monastery guidelines
Original title: 'pattern matching'

Replies are listed 'Best First'.
Re: How Do I Skip Spaces At The End of Words Using A Regex?
by davido (Cardinal) on Sep 18, 2006 at 04:20 UTC

    You've told us what you want to ignore. Now tell us what you want to match. What are you actually trying to do? Give us a good description and sample of the input data, and explain what you wish to match, and to what outcome. Another component of your question ought to tell us what you're wishing to use pattern matching for. You could be matching to reject non-compliant strings, or you could be matching to capture a portion of a string, or you could be matching to verify that a string contains a necessary component, or, or, or... (there are lots of reasons, and they partially dictate the solution).

    Update:
    I see you've added to your original post... will try to give a more thorough answer once I comprehend what you've added.

    Update2:
    Try changing my $y = "....... to the following:

    my $y = quotemeta( '...........' );

    The problem is that many of the characters in your $y string are special characters, interpreted in a special way within regular expressions, unless you "escape" the special characters. quotemeta does that for you.


    Dave

Re: How Do I Skip Spaces At The End of Words Using A Regex?
by Zaxo (Archbishop) on Sep 18, 2006 at 04:47 UTC

    If you change the pattern match to

    if ( $x =~ /\Q$y\E\s+/ig ) { # . . . }
    it should work fine.

    After Compline,
    Zaxo

Re: How Do I Skip Spaces At The End of Words Using A Regex?
by ikegami (Patriarch) on Sep 18, 2006 at 04:57 UTC

    In addition to using quotemeta or \Q...\E, the g should be removed.

    Furthermore, /\Q$y\E\s+/i is the same as /\Q$y\E\s/i (because of the lack of anchors and captures), and /\Q$y\E/i would surely suffice in this case.

    Lastly, if ($x =~ /\Q$y\E/i) is slower than if (index(lc($x), lc($y)) >= 0).

      It may be slower in some circumstances.    :-)
      $ perl -le' my $x = "set manager logport [<0-10000>] set manager secondary ip A.B.C.D set mgmtport auto set mgmtport speed (10|100) duplex (full|half) set portsettletime <0-300> set sensor gateway A.B.C.D set sensor ip A.B.C.D E.F.G.H"; my $y = q/set mgmtport speed (10|100) duplex (full|half)/; use Benchmark q/cmpthese/; cmpthese -10, { regex => q[my $z = 1 if $x =~ /\Q$y\E\s/i], index => q +[my $z = 1 if index( lc $x, lc $y ) >= 0] }; ' Rate index regex index 1475956/s -- -24% regex 1932798/s 31% --
      Update: The regex gets even faster if you use the /o option.
      Rate index regex index 1431366/s -- -33% regex 2139276/s 49% --
      /\Q$y\E/i would surely suffice in this case

      In which case, /\Q$y/i would suffice :)

Re: How Do I Skip Spaces At The End of Words Using A Regex?
by rsriram (Hermit) on Sep 18, 2006 at 05:10 UTC

    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.