Regular Expression
Regular Expression format: special character + normal character
1. find express which start with FROM or SUBJECT:
^(FROM|SUBJECT):
2. replace <emphasis>ip address</emphasis> to <inet>ip address</inet>
(perl) s!<emphasis>([0-9]+(\.[0-9]+){3})</emphasis>!<inet>$1</inet>!
Special Character
1. ^: start of line
2. $: end of line
^$: match empty line
^cat: match line start with "cat"
cat$: match line end with "cat"
^cat$: match line only contains "cat"
3. []: character group
A. [0-9]: match all number
B. [a-z]: match all lower letter
C. [A-Z]: match all upper letter
D. [0-9A-Za-z]: match all number, lower letter and upper letter
gr[ae]y --> gray / grey
<H[123]> --> <H1> / <H2> / <H3>
<H[1-3]> --> <H1> / <H2> / <H3>
4. [^]: get rid of character group
[^1-6]: no match 1~6;
5. . : match one any item
03.19.76 --> 03/19/76; 30-19-76
6. |: or
gr(a|e)y --> gray; grey
Bob|Robert --> Bob; Robert
7. \b: separate words
\bcat\b: match "cat" word
8.{}: match duplicate times
[a-zA-Z]{1,5}: match stock code 1 to 5 letter
? == {0,1}
9. \: match following word not special character
10. match: =~//
Example:
print "Enter a temperature in Celsius: \n";
$celsius=<STDIN>;
chomp($celsius);
if($celsius=~m/^[0-9]+$/)
{
$fahrenheit=($celsius*9/5)+32;
print "$celsius C is $fahrenheit F. \n"
}
else
{
print "Expecting a number."
}
perl regular expression
\t --> tab
\n --> new line
\r --> enter
\s --> any empty space
\S --> any character except \s
\w --> [a-zA-Z0-9]
\W --> any character except \w
\d--> [0-9]
\D --> any character except \d
Comments
Post a Comment