关于模式匹配的分类

上一篇 / 下一篇  2011-05-17 14:56:36

  There are three types of patterns:globbing,regular expressions, andexact.

  1. Globbing Patterns(for SQL)

 Most people are familiar with globbing as it is utilized in filename expansion at a DOS or Unix/Linux command line such asls*.c. In this case, globbing is used to display all the files ending with a.cextension that exist in the current directory. Globbing is fairly limited. There are three types of globbing pattens we are familiar.

*which translates to “match anything,” i.e., nothing, a single character, or many characters.

[ ](character class) which translates to “match any single character found inside the square brackets.” A dash (hyphen) can be used as a shorthand to specify a range of characters (which are contiguous in the ASCII character set). A few examples will make the functionality of a character class clear:

[aeiou]matches any lowercase vowel

[0-9]matches any digit

[a-zA-Z0-9]matches any alphanumeric character

?any single character

2.Regular Expression Patterns

PATTERNMATCH
.any single character
[ ]character class: any single character that appears inside the brackets
*quantifier: 0 or more of the preceding character (or group)
+quantifier: 1 or more of the preceding character (or group)
?quantifier: 0 or 1 of the preceding character (or group)
{1,5}quantifier: 1 through 5 of the preceding character (or group)
|alternation: the character/group on the left or the character/group on the right
( )grouping: often used with alternation and/or quantifier

An example:description text in the web page with

Sunrise: *[0-9]{1,2}:[0-9]{2} [ap]m

Let’s examine the regular expression above one part at a time:

Sunrise:*The stringSunrise:followed by 0 or more spaces
[0-9]{1,2}1 or 2 digits (for the hour of the day)
:The character:(no special characters involved)
[0-9]{2}2 digits (for the minutes) followed by a space
[ap]m“a” or “p” followed by “m” (am or pm)

3.Exact Patterns

if you needed to look for an actual asterisk character (which is special for both globbing and regular expression patterns), the exact pattern would be one way to do that. For example, if you wanted to select an item labeled “Real *” from a dropdown, the following code might work or it might not. The asterisk in theglobbingpattern will match anything or nothing. So, if there was an earlier select option labeled “Real Numbers,” it would be the option selected rather than the “Real *” option.it equals "Real \*" in Regular Expression Patterns


TAG:

 

评分:0

我来说两句

Open Toolbar