Regular Expressions

Substitutions

To look for a pattern in the string and replace it with something else, use the following syntax:
$string =~ s/PATTERN/REPLACEMENT_STRING/;

To do case-insensitive pattern matching, append an i to the substitution.

$string =~ s/PATTERN/REPLACEMENT_STRING/i;

If you want the replacement to operate on all possible matches instead of just the first match, append a g to the substitution.

$string =~ s/PATTERN/REPLACEMENT_STRING/g;
To remove all substrings matching the pattern write:
$string =~ s/PATTERN//g;

Note that $string is changed after these operations!

Examples

# replace the first occurence of the word ID with "Identification";
$swiss_prot_line =~ s/ID/Identification/;


# replace all occurences of BIU with Bar Ilan University
$addresses =~ s/BIU/Bar Ilan University/g;

# remove all spaces, tabs etc. from a sequence entry
$seq =~ s/\s+//g;


Table of Contents.
Previous | Next.