Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

正则表达式 #39

Open
storyflow opened this issue Aug 28, 2018 · 3 comments
Open

正则表达式 #39

storyflow opened this issue Aug 28, 2018 · 3 comments

Comments

@storyflow
Copy link
Owner

storyflow commented Aug 28, 2018

学习资料:https://juejin.im/post/5b96a8e2e51d450e6a2de115

@storyflow
Copy link
Owner Author

storyflow commented Sep 11, 2018

元字符

元字符 说明
. 匹配除换行符以外的任意字符
\w 匹配字母或数字或下划线或汉字
\s 匹配任意的空白符
\d 匹配数字
\b 匹配单词的开始或结束
^ 匹配字符串的开始
$ 匹配字符串的结束

匹配有abc开头的字符串:

\babc或者^abc

匹配8位数字的QQ号码:

^\d\d\d\d\d\d\d\d$

匹配1开头11位数字的手机号码:

^1\d\d\d\d\d\d\d\d\d\d$

@storyflow
Copy link
Owner Author

storyflow commented Sep 11, 2018

重复限定符

语法 说明
* 重复零次或更多次
+ 重复一次或更多次
? 重复零次或一次
{n} 重复n次
{n,} 重复n次或更多次
{n,m} 重复n到m次

匹配8位数字的QQ号码:

1^\d{8}$

匹配1开头11位数字的手机号码:

1^1\d{10}$

匹配银行卡号是14~18位的数字:

1^\d{14,18}$

匹配以a开头的,0个或多个b结尾的字符串

1^ab*$

@storyflow
Copy link
Owner Author

storyflow commented Sep 11, 2018

分组

正则表达式中用小括号()来做分组,也就是括号中的内容作为一个整体。

匹配字符串中包含0到多个ab开头:

^(ab)*

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant