正则表达式,Regular Expressions,简称 RE,主要用于字符串处理、表单验证等,很多编辑器例如 Vim 都内嵌支持 RE 搜索,非常高效方便。收集了一些常用的表达式,做个积累。

匹配空白行:\n\s*\r

匹配首尾空白字符(包括空格、制表符、换页符等等):^\s*|\s*$

匹配Email地址:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

匹配网址URL:[a-zA-z]+://[^\s]*

匹配身份证(15或18位):\d{15}|\d{18}

匹配ip地址:\d+\.\d+\.\d+\.\d+

匹配特定字符串:
        ^[A-Za-z]+$ //匹配由26个英文字母组成的字符串
        ^[A-Z]+$ //匹配由26个英文字母的大写组成的字符串
        ^[a-z]+$ //匹配由26个英文字母的小写组成的字符串
        ^[A-Za-z0-9]+$ //匹配由数字和26个英文字母组成的字符串
        ^\w+$ //匹配由数字、26个英文字母或者下划线组成的字符串

Tags:

Related posts

Tags: .
Home

No Comments Now!

Be the first to comment on this entry.

Leave a comment

Name(required)
Mail (required),(will not be published)
Website(recommended)

Fields in bold are required. Email addresses are never published or distributed.

Some HTML code is allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
URLs must be fully qualified (eg: http://hanghang.name),and all tags must be properly closed.

Line breaks and paragraphs are automatically converted.

Please keep comments relevant. Off-topic, offensive or inappropriate comments may be edited or removed.