Documentos de Académico
Documentos de Profesional
Documentos de Cultura
/PATTERN/
Regular Expressions(3)
/to?ols/ the character before ? is optional. Thus, there are only
two matching strings tools and tols.
/to{2}ls/ the number in {} tells about the repetitions
{count}
{min,max} - Match at least min but not more than max times
{min,}
Negating:
[^abc] any char except a,b,c
1. Matching of vowels
2. Matching of consonants
3. Anything other than non Numbers
Diff between \D and [^\d]
Anchors
/a\^bc/ - matches \^
/[^abc]/ - negating
\B - negative of \b
/((hello|hi) (world|Perl))/
\1 refers to (hello|hi) (world|Perl)
\2 refers to (hello|hi)
\3 refers to (world|Perl)
=~
while( <STDIN> ){
if( /hello/ ){
}
@words = split /\s+/, $str;
}
}
Examples
$date="12 10
10";
if($date=~ /(\d+)/){
print
$1.":".$2.":".$3.":\n";
}
#output ($2 and $3 are empty):
#12:::
if($date=~ /(\d+)(\s+\1)+/){
print $1.":".$2.":".$3.":\n";
}
#output (notice $3 is empty):
#10:
10::
$str="Hello World";
if($str=~ /((Hello|Hi) (World|Perl))/)
{
print $1.":".$2.":".$3.":\n";
}
#output:
#Hello World:Hello:World:
$str="Hello Perl Hi";
if($str=~ /((Hello|Hi) (World|Perl)) \
1/){
print $1.":".$2.":".$3.":\n";
}
#output: non
$str="Hello Perl Hi";
if($str=~ /((Hello|Hi) (World|Perl)) \
1/){
print $1.":".$2.":".$3.":\n";
}
#output:
#Hi Perl:Hi:Perl:
Examples
1. What is it?
/^0x[0-9a-fA-F]+$/
Examples
3. Make a pattern that matches any line of input that has
the same word repeated two (or more) times in a row.
Whitespace between words may differ.
Example
1. /\w+/
#matches a word
2. /(\w+)/
3. /(\w+)\1/
#two times
Lets try
1) Write a regular expression that identifies a 24-hour
clock. For example: 0:01, 00:20, 15:00, 23:59
Negated Match
Negation
if( $str =~ /hello/){
$`
Caution: Never use this in your script if you really dont need
this.
ccc
dddd;
#result is aaa:bbb:ccc:dddd
aaa b ->
aaa, b
aaa b ->
aaa, b
Grep