码迷,mamicode.com
首页 > 其他好文 > 详细

match

时间:2015-08-18 18:19:57      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:

int match(str pattern, str text)

match(‘<:D+>‘, s);判断字符串是否全部为数值

match(‘<:A+>‘, s);判断字符串是否全部为字符

match(‘<:N+>‘, s);判断字符串是否全部为数字或字符

        

Character

Description

\

A backslash causes a specific character to be matched. Remember to escape backslashes. For example:

技术分享

Copy Code

    match("ab$cd","ab$cd");    //returns 0
    match("ab\$cd","ab$cd");   //returns 0 - the backslash is not escaped
    match("ab\\$cd","ab$cd");  //returns 1 - the backslash and dollar sign are escaped

< or ^

A ‘less than‘(<) sign or a circumflex (^) at the start of an expression is used to match the start of a line. For example: 以什么打头的

技术分享

Copy Code

    match("<abc","abcdef"); //returns 1
    match("<abc","defabc"); //returns 0
    match("^abc","abcdef"); //returns 1
    match("^abc","defabc"); //returns 0

>

A ‘greater than‘(>) sign at the end of the expression is used to match the end of a line. For example: 以什么结尾

技术分享

Copy Code

    match("abc>","abcdef"); //returns 0
    match("abc>","defabc"); //returns 1

? or .

A question mark (?) or a full stop (.) will match any character. For example:

技术分享

Copy Code通配符

    match("abc.def","abc#def"); //returns 1
    match("colou?r","colouXr"); //returns 1

:a

Sets the match to letters. For example: 字母

技术分享

Copy Code

    match("ab:acd","ab#cd");   //returns 0
    match("ab:acd","abxyzcd"); //returns 0
    match("ab:acd","abxcd");   //returns 1

:d

Sets the match to numeric characters. For example: 数字

技术分享

Copy Code

    match("ab:dcd","ab3cd");   //returns 1
    match("ab:dcd","ab123cd"); //returns 0
    match("ab:dcd","abcd");    //returns 0

:n

Sets the match to alphanumeric characters. For example: 数字或字符

技术分享

Copy Code

    match("ab:ncd","ab%cd"); //returns 0
    match("ab:ncd","ab9cd"); //returns 1
    match("ab:ncd","abXcd"); //returns 1

:SPACE

Where SPACE is the character ‘ ‘. Sets the match to blanks, tabulations, and control characters such as Enter (new line). For example: 空格,表格,控制符(回车)

技术分享

Copy Code

    match("ab: cd","ab cd");   //returns 1
    match("ab: cd","ab\ncd");  //returns 1
    match("ab: cd","ab\tcd");  //returns 1
    match("ab: cd","ab   cd"); //returns 0 - only the first space is matched

*

An expression followed by an asterisk requires a match for none, one, or more occurrences of the preceding expression. For example: 全部由*前的字符串中的没有,一个,多个组成

技术分享

Copy Code

    match("abc*d","abd");    //returns 1
    match("abc*d","abcd");   //returns 1
    match("abc*d","abcccd"); //returns 1
    match("abc*d","abxd");   //returns 0

+

An expression followed by a plus (+) sign requires a match for one or more occurrences of the preceding expression. For example: 全部由*前的字符串中的一个,多个组成

技术分享

Copy Code

    match("abc+d","abd");    //returns 0
    match("abc+d","abcd");   //returns 1
    match("abc+d","abcccd"); //returns 1
    match("abc+d","abxd");   //returns 0

-

An expression followed by a minus (-) sign requires a match for one or no occurrences of the preceding expression. Basically, the preceding expression is optional. For example:

全部由*前的字符串中的没有,一个组成

技术分享

Copy Code

    match("colou-r","color");  //returns 1
    match("colou-r","colour"); //returns 1

[]

Matches a single character with any character contained within the brackets.A range of characters can be specified by two characters separated by ‘-‘ (minus). For example, "[a-z]" matches all letters between a and z, [0-9] matches a digit, [0-9a-f] matches a hexadecimal digit. 字符串范围

技术分享

Copy Code

    match("[abc]","apple");   //returns 1 - matches the ‘a‘ in apple
    match("[abc]","kiwi");    //returns 0 - kiwi does not contain an a, b, or c
    match("gr[ae]y","grey");  //returns 1 - also matches "gray"
    match("gr[ae]y","graey"); //returns 0 - only one character between "gr" and "y" is matched.

[^]

If the first character in a text within square brackets is a circumflex (^), the expression matches all characters except those contained within the brackets. 不包括字符串范围

技术分享

Copy Code

    match("[^bc]at","bat"); //returns 0
    match("[^bc]at","hat"); //returns 1
    match("[^abc]","bat");  //returns 1 - anything but a, b, or c is matched. The t is matched

match

标签:

原文地址:http://www.cnblogs.com/AllenFeng/p/4739691.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!