正则使用从0开始学习(二)

一些有用的正则的尝试和收集:
以下是以js规则的表达式

1、匹配中文

var s = '我爱中国';
var reg = /[\u4e00-\u9fa5]/;
var result = s.match(reg);
匹配结果: ["我", index: 0, input: "我爱中国"]
// 全局匹配
var reg = /[\u4e00-\u9fa5]/g;
var result = s.match(reg);
匹配结果: (4) ["我", "爱", "中", "国"]
// 其他
'我爱中国'.match(/[\u4e00-\u9fa5]*/);
'我爱中国'.match(/[\u4e00-\u9fa5]+/);
匹配结果: ["我爱中国", index: 0, input: "我爱中国"]
'我爱中国'.match(/[\u4e00-\u9fa5]?/);
匹配结果:["我", index: 0, input: "我爱中国"]
'我爱中国'.match(/[\u4e00-\u9fa5](.*?)/);
匹配结果: ["我", "", index: 0, input: "我爱中国"]

2、匹配邮箱

var s = 'm1212@sina.com'
var reg = /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/g
var result = s.match(reg);
匹配结果:["m1212@sina.com"]

3、匹配URL

var s = 'http://www.baidu.com'
s.match(/[a-zA-z]+://[^\s]*/);
匹配位置:0
匹配结果:http://www.baidu.com

4、匹配@abc@ 或者 #abc#类型

var s = '#fdsa# #fdsabb#'
s.match(/^(@|#)[^\s]*?(@|#)/g);
匹配结果: ["#fdsa#", "#fdsabb#"]

以上并不能匹配’# fdsa#’这种类型,要匹配这种类型,需要包含\s

var s = '#fdsa# #fdsabb#'
s.match(/^(@|#)(.*?)(@|#)/g);
匹配结果: ["#fdsa#", "#fdsabb#"]

5、匹配QQ

var s = '11212121'
s.match(/[1-9][0-9]{4,}/);
匹配结果: 11212121

5、匹配邮编

var s = 'afa4614612afa'
s.match(/[1-9]\d{5}(?!\d)/);
匹配结果: 4614612

6、匹配身份证号

var s = '411528199208132243';
var reg = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/;
s.match(reg);
匹配结果: 411528199208132243

6、匹配年-月-日格式

var s = '2017-11-14';
var reg = /([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))/;
s.match(reg);
匹配结果: 2017-11-14

7、匹配整数

var s = '121';
var reg = /^[1-9]\d*$/
s.match(reg);
匹配结果: 121

8、匹配IP地址

var s = '192.168.1.11';
var reg = /((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?)))/
s.match(reg);
匹配结果: 192.168.1.11

9、匹配浮点数

var s = '0.1111121';
var reg = /^-[1-9]\d*\.\d*|-0\.\d*[1-9]\d*$/
s.match(reg);
匹配结果: 0.1111121
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Excel VBA 正则表达式可以用于处理文本数据,根据指定的模式匹配并提取所需信息。下面是一个从实例开始的参考模板: 1. 首先,我们需要在 VBA 中启用 "Microsoft VBScript Regular Expressions" 引用。在 VBA 编辑器中,点击 "工具" -> "引用",然后勾选 "Microsoft VBScript Regular Expressions"。 2. 创建一个正则表达式对象并定义要匹配的模式。例如,如果我们想要从一个字符串中提取所有的邮箱地址,我们可以使用以下代码: ```VBA Dim regEx As New RegExp Dim strPattern As String Dim strInput As String strPattern = "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b" strInput = "这是一个字符串,其中包含了多个邮箱地址 [email protected], [email protected], [email protected]" With regEx .Global = True .IgnoreCase = True .Pattern = strPattern End With ``` 3. 接下来,我们可以使用正则表达式对象的 Match 方法来查找并获取匹配的结果。例如,我们可以使用循环来提取所有的邮箱地址: ```VBA Dim matches As MatchCollection Set matches = regEx.Execute(strInput) Dim match As Match For Each match In matches MsgBox match.Value Next match ``` 4. 在上述示例中,我们使用了 MsgBox 函数来显示每个匹配的邮箱地址。你可以根据自己的需求进行进一步的处理,例如将匹配的结果存储到一个数组或列表中。 使用正则表达式可以极大地简化文本数据的处理过程,尤其是对于复杂的模式匹配。注意,正则表达式语法很灵活,可以根据不同的需求进行调整。为了更好地理解和使用正则表达式,你可以参考相关的正则表达式教程或手册。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值