programming-examples/perl/Regular_Expression/Bracket.pl
2019-11-15 12:59:38 +01:00

71 lines
1.3 KiB
Perl

#Square brackets ([ and ]) delimits a range of characters.!
[aA] means either a or A.
[a-z] matches any lowercase character.
[0-9] matches any digit.
[0-9a-zA-Z] for characters commonly used in variable names.
You can combine the brackets with other patterns.
Pattern Interpretation
/[aA]/ Matches against a or A.
/[aA]+/ Matches one or more instances of a or A.
/[aA]*/ Matches zero or more instances of a or A.
/[aA]?/ Matches zero or one instance of a or A.
/[^aA]/ Returns true if any character is found that is not a or A.
/[aA]|[bB]/ Matches an instance of a or A or b or B; redundant in this case, as it is the same as /[aAbB]/.
#The [] special characters enable you to define patterns that match one of a group of alternatives.!
For example, the following pattern matches def or dEf:
/d[eE]f/
#Combine [] with + to match a sequence of characters of any length.!
/d[eE]+f/
This matches all of the following:
def
dEf
deef
dEef
dEEEeeeEef
#The bracketed character class!
while(<DATA>){
print if /[A-Za-z0-9_]/;
}
__DATA__
Tom 101
Jack 201
Nart 301