Regular Expression 基础

Regular Expression:正则表达式 又称 正规表达式

计算机科学的一个概念。正则表达式使用单个字符串来描述、匹配一系列符合某个句法规则的字符串。在很多文本编辑器里,正则表达式通常被用来检索、替换那些符合某个模式的文本。

学习前提示
换行符‘\n’和回车符‘\r’

顾名思义,换行符就是另起一行,回车符就是回到一行的开头,所以我们平时编写文件的回车符应该确切来说叫做回车换行符

‘\n‘ 10 换行(newline)
‘\r‘ 13 回车(return)

也可以表示为‘\x0a‘和‘\x0d‘.(16进制)

在windows系统下,回车换行符号是"\r\n".但是在Linux等系统下是没有"\r"符号的。

在解析文本或其他格式的文件内容时,常常要碰到判定回车换行的地方,这个时候就要注意既要判定"\r\n"又要判定"\n"。

写程序时可能得到一行,将其进行trim掉‘\r‘,这样能得到你所需要的string了。

正则表达式 元字符

<table border="1" cellpadding="0">
        <tr>
            <th>符号</th>
            <th>描述</th>
        </tr>
        <tr>
            <td>^</td>
            <td>匹配字符串的开始</td>
        </tr>
        <tr>
            <td>$</td>
            <td>匹配字符串的结束</td>
        </tr>
        <tr>
            <td>\s</td>
            <td>任何空白字符串</td>
        </tr>
        <tr>
            <td>\S</td>
            <td>热火非空白字符串</td>
        </tr>
        <tr>
            <td>\d</td>
            <td>匹配一个数字字符,等价于[0-9]</td>
        </tr>
        <tr>
            <td>\D</td>
            <td>匹配数字字符以外的任何字符,等价于[^0-9]</td>
        </tr>
        <tr>
            <td>\w</td>
            <td>匹配一个数字、下划线或字母字符,等价于[A-Za-z_0-9]</td>
        </tr>
        <tr>
            <td>\W</td>
            <td>匹配任何非单字字符,等价于[^A-Za-z_0-9]</td>
        </tr>
        <tr>
            <td>.</td>
            <td>除了换行符以外的任意字符</td>
        </tr>
    </table>

注:在正则表达式中,我们使用大小写来表示取反
表格里面这个^符号它表示"非"

一般用\w来匹配用户名、邮箱名
一般用\S来匹配任何非空的
一般用.来匹配任意字符

例子

邮箱:[email protected]
QQ:234534324
邮箱:^\[email protected]\w.[a-zA-Z]$
QQ:\d{5,12} {}表示出现的次数,这里是5到12次

上面的写法是错误的!因为只匹配了单个字符!!

匹配符号

<table border="1">
        <tr>
            <th>匹配符号</th>
            <th>描述</th>
        </tr>
        <tr>
            <td>{n}</td>
            <td>匹配前面一项n次</td>
        </tr>
        <tr>
            <td>{n,}</td>
            <td>匹配前面一项n次,或者多次</td>
        </tr>
        <tr>
            <td>{n,m}</td>
            <td>匹配前面一项n次,但不超过m次</td>
        </tr>
        <tr>
            <td>*</td>
            <td>匹配前面一项0次或者多次,相当于{0,}</td>
        </tr>
        <tr>
            <td>+</td>
            <td>匹配前面一项1次或者多次,相当于{1,0}</td>
        </tr>
        <tr>
            <td>?</td>
            <td>匹配前面一项0次或者1次,相当于{0,1}</td>
        </tr>
    </table>

所以:

邮箱 [email protected](.cn)

正确的正则表达式为: ^\[email protected]\w+\.[a-zA-Z]{2-3}(\.[a-zA-Z]{2-3}?)

注:匹配符号匹配的是它前面的单个字符
.表示匹配除了\s\n(换行符)以外的任意字符,所以这里要用\转义下

RegularExpresssion正则表达式(RegExp)

语法
var reg1=new RegExp(/^\w{2,3}/);
var reg2=new RegExp("red","i")(g-全局查找 m-多行查找)
(简写)
var reg1=/^\w{2,3}/;
var reg2=/red/i;

注:区别,第一种方法里面的参数是我们的正则表达式,第二种方法里面的参数是一个普通的字符串。

例子

var regQQ=/^\d{5,12}$/;
var strQQ="55"

var result=regQQ.test(strQQ);
注 test():验证目标字符串是否符合正则表达式的规则,返回值为布尔类型

String对象支持正则表达式的方法

search(正则表达式):返回匹配的位置
match (正则表达式):返回匹配的字符串
replace (正则表达式,要替换字符串): 返回替换后的结果

注:这个三个方法不是正则表达式的方法,而是string对象的方法

时间: 2024-08-03 19:22:00

Regular Expression 基础的相关文章

9.Regular Expression Matching (String; DFS)

Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be

刷题10. Regular Expression Matching

一.题目说明 这个题目是10. Regular Expression Matching,乍一看不是很难. 但我实现提交后,总是报错.不得已查看了答案. 二.我的做法 我的实现,最大的问题在于对.*的处理有问题,始终无法成功. #include<iostream> using namespace std; class Solution{ public: bool isMatch(string s,string p){ bool result = true; if(s.length()<=0

LeetCode 10. Regular Expression Matching

https://leetcode.com/problems/regular-expression-matching/description/ Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover

Python正则表达式Regular Expression基本用法

资料来源:http://blog.csdn.net/whycadi/article/details/2011046   直接从网上资料转载过来,作为自己的参考.这个写的很清楚.先拿来看看. 1.正则表达式re模块的基本函数. (1)findall函数的用法 findall(rule,target[,flag])是在目标字符串中找到符合规则的字符串.参数说明:rule表示规则,target表示目标字符串,[,flag]表示的是规则选项.返回的结果是一个列表.若没找到符合的,是一个空列表. 如: 因

Java [leetcode 10] Regular Expression Matching

问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. http://i.cnblogs.com/EditPosts.aspx?opt=1 The matching should cover the entire input string

【leetcode】Regular Expression Matching (hard) ★

Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be

[LeetCode] Regular Expression Matching(递归)

Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be

Python正则表达式 re(regular expression)

1. 点. .: 代表一个字符 (这个跟linux的正则表达式是不同的,那里.代表的是后面字符的一次或0次出现) 2. 转义 \\ 或者 r'\': 如 r'python\.org' (对.符号的转义) 3. ^ 非或叫做排除 如[^abc]: 任何以非a,b,c的字符 4. | 选择符 如python|perl (从python和perl选择一个) 也可以: p(ython|erl) 5. ? 可选项 如: r'(http://)?(www\.)?python\.org' (http://和w

[LeetCode] 10. Regular Expression Matching ☆☆☆☆☆

Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be