Labels

Tuesday, January 22, 2013

Some useful regular expressions

[ ]        Denotes a set of possible character matches.


( )        Groups a series of pattern elements to a single element.
            When you match a pattern within parentheses, you can use
            any of $1, $2, ... later to refer to the previously matched pattern.


EXAMPLE: Insert one space between every English character and number.

                    $string = "asdf112fds321asd";

                    $string =~ s/([0-9])([A-Za-z])/$1 $2/g;

                    $string =~ s/([A-Za-z])([0-9])/$1 $2/g;

                    print "$string";


.           Match any single character except "\n"

+          Match one pattern one or many times

*          Match one pattern zero or many time

EXAMPLE: match a string that contain parenthesis.

                      $string =~ m/\(.+\)/;

No comments:

Post a Comment