8、VBA使用正则表达式拆分内容

1 重点内容

01 正则表达式文本

myreg.pattern=".*[^学历规划师]:\s+.*"

02  执行语句

regexp.execute(s)

使用regexp对象那个已经设置好的表达式(pattern属性)在字符窜s中查找符合t条件的文本

03  替换

regexp.replace(s,r)并未修改原文本是,而是将替换后的节骨作为一个新的字符窜

cells(i,2)=myreg.(s,replace,“$1:$2“)

#$n 代表din个圆括号中捕获的内容

2 应用案例

option explicit

Sub replacedemo()

dim i&,s$
dim myreg as Object
dim myMatches as Object,mymatch as object

'将内容赋值变量

s=range("g2").value

‘创建一个regexp类型对象,可用于调用处理正则表达式
'
set myReg= Createobject ("Vbscript.regexp") 

'获取正则表达式文本

myseg.pattern="(.*)[^学历规划师]:(\s+).*"

# 导出所有符合表达式的内容:

myReg.Global=true

# 应用execute方法执行表达式得到数组

set ymatches=myreg.execute(s)




# 将括号内的内容进行替换

$n 只输出对应的括号内的内容

cell(2,2)myreg.replace(s,"$1:$2")



#遍历matches里边所有符合表达式的内容

for each mymatche in mymatches
	
    cells(i,1)=myMatche.submatches(0)
cells(i,2)=myMatche.submatches(0)

i=i+1

next mymatc

end sub

猜你喜欢

转载自blog.csdn.net/qq_36327687/article/details/84940159