![]() |
||||||||||
|
||||||||||
|
|
Perl: Examples of Regular Expressions
# convert to UPPERCASE
# returns $line = $line (all in uppercase) $line =~ tr/a-z/A-Z/; # convert to lowercase # returns $line = $line (all in lowercase) $line =~ tr/A-Z/a-z/; # remove leading whitespace # returns $line with no leading whitespace $line =~ s/^\s+//; # remove trailing whitespace # returns $line with no trailing whitespace # note: this will also remove \n $line =~ s/\s+$// # check for Y2K date (4-digit year specified) # format exactly: nn/nn/nnnn or nn-nn-nnnn # returns true if proper format ($line1 =~ m/[0-9]{2}[\/|-][0-9]{2}[\/|-][0-9]{4}/) # find word that is bolded # returns: $1 = 'text' $line = "This is some <b>text</b> with HTML <tags> and "; $line =~ m/<b>(.*)<\/b>/i; # convert <STRONG> to <B> $line =~ s/<(\/?)strong>/<$1b>/gi; # strip HTML tags (not the best way) # returns $line = 'This is some with HTML and words' $line = "This is some <b>text</b> with HTML <tags> and words"; $line =~ s/<(.*?)>//gi; See also for more about regular expressions:
|
| © 1997-2007. astonishinc.com All Rights Reserved. |