Square brackets ([ and ]) delimits a range of characters. : bracket « Regular Expression « 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]/.

   
    
    
    
  








Related examples in the same category

1.The [] special characters enable you to define patterns that match one of a group of alternatives.
2.Combine [] with + to match a sequence of characters of any length.
3.The Bracketed Character Class