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

Okay, total beginner's question, BUT: What can you use to find particular tokens in a file? Let's say that you have a file that has sets of parentheses and you want to parse it to grab everything that comes between a complete set of parentheses (like this) and you want to grab the text outside of the parentheses as well... Which reg ex would work for something like that? Thanks!

Replies are listed 'Best First'.
Re: Parsing with reg ex
by gjb (Vicar) on Oct 23, 2002 at 07:25 UTC
Re: Parsing with reg ex
by kabel (Chaplain) on Oct 23, 2002 at 04:59 UTC
    this code does not find particular tokens, it just finds anything inside parenthesis and prints it out. this is meant to be a startingpoint.

    use strict; use warnings; # # slurp file # my $file_contents = ""; { local $/; $file_contents = <DATA>; } # # "parse" file # while ($file_contents =~ m/\((.*?)\)/sg) { my $match = $1; $match =~ s/\n//g; print "[$match]\n"; } __DATA__ prematch (match) postmatch adsf (madfa tch) asdf

    the meaning of the special variables can be found in perlvar.
    also have a look inside the tutorials section.

    UPDATE: added backreferencing () for $1 (thx to robartes)
      So your code could also work for * or % or whatever... it would just be a matter of changing the symbol, right? Thank you.