Regex 合并正则表达式并填充一定数量的单元格
我在Excel中有一个单元格,它在单元格A1中保存一个长字符串:Regex 合并正则表达式并填充一定数量的单元格,regex,vba,excel,Regex,Vba,Excel,我在Excel中有一个单元格,它在单元格A1中保存一个长字符串: "ABC12+BED58,YZ001" 我有以下正则表达式来匹配字符串中的一些特定变量 strPattern = "[A-Z]{1,3}[0-9]{2,4}" 基本上,我需要编写一个宏或一个函数(我更喜欢一个函数)来填充单元格A2、A3、A4,如下所示: ABC12 BED58 YZ001 问题是,字符串中有一个数量不确定的参数(例如,它可以一直通过A200) 我正在考虑一个函数get\u n\u variables(str
"ABC12+BED58,YZ001"
我有以下正则表达式来匹配字符串中的一些特定变量
strPattern = "[A-Z]{1,3}[0-9]{2,4}"
基本上,我需要编写一个宏或一个函数(我更喜欢一个函数)来填充单元格A2、A3、A4,如下所示:
ABC12
BED58
YZ001
问题是,字符串中有一个数量不确定的参数(例如,它可以一直通过A200)
我正在考虑一个函数get\u n\u variables(str,n)
,它将返回第n个唯一的匹配
这是我到目前为止的进度,但函数返回#VALUE
函数simpleCellRegex(Myrange作为范围)作为字符串
Dim regEx作为新的RegExp
作为字符串的Dim strPattern
像弦一样的模糊的条纹
暗匹配作为对象
strPattern=“[A-Z]{1,3}[0-9]{2,4}”
如果strPattern“”则
strInput=Myrange.Value
strReplace=“”
用正则表达式
.Global=True
.MultiLine=True
.IgnoreCase=False
.Pattern=strPattern
以
如果正则表达式测试(strInput),则
Set matches=regEx.Execute(strInput)
simpleCellRegex=匹配项(0)。子匹配项(0)
其他的
simpleCellRegex=“不匹配”
如果结束
如果结束
端函数
来自:
不能在单元格中放置函数来更改其他单元格。函数不是这样工作的
因此,它应该是子,如下所示,例如(使用输入字符串输出所选单元格下的匹配项):
Sub-simpleCellRegex()
Dim regEx作为新的RegExp
作为字符串的Dim strPattern
像弦一样的模糊的条纹
将匹配项设置为MatchCollection
暗i与长,cnt与长
strPattern=“[A-Z]{1,3}[0-9]{2,4}”
cnt=1
如果strPattern“”则
strInput=ActiveCell.Value
strReplace=“”
用正则表达式
.Global=True
.MultiLine=True
.IgnoreCase=False
.Pattern=strPattern
以
如果正则表达式测试(strInput),则
Set objMatches=regEx.Execute(strInput)
对于i=0到objMatches.Count-1
ActiveCell.Offset(cnt).Value=objMatches.Item(i)
cnt=cnt+1
下一个
如果结束
如果结束
端接头
输出:
如果使用数组,实际上仍然可以使用函数
- 选择
B1:D1
- 输入此公式
=simpleCellRegex(A1)
,然后按CTRL+SHIFT+enter
如果您不知道输入的单元格中有多少个匹配项多于可能存在的匹配项
代码
可能重复-请参阅关于拆分值的部分。@KenWhite我更新了我的问题以显示我的进度,我相信问题已接近尾声,但我的代码不知何故无法运行,我需要调试方面的帮助除了+和之外,您还有其他分隔符吗?可以是任何分隔符。我只需要根据正则表达式匹配变量,然后在列的不同单元格中列出它们
Function simpleCellRegex(Myrange As Range) As String
Dim regEx As New RegExp
Dim strPattern As String
Dim strInput As String
Dim matches As Object
strPattern = "[A-Z]{1,3}[0-9]{2,4}"
If strPattern <> "" Then
strInput = Myrange.Value
strReplace = ""
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = strPattern
End With
If regEx.Test(strInput) Then
Set matches = regEx.Execute(strInput)
simpleCellRegex = matches(0).SubMatches(0)
Else
simpleCellRegex = "Not matched"
End If
End If
End Function
Sub simpleCellRegex()
Dim regEx As New RegExp
Dim strPattern As String
Dim strInput As String
Dim matches As MatchCollection
Dim i As Long, cnt As Long
strPattern = "[A-Z]{1,3}[0-9]{2,4}"
cnt = 1
If strPattern <> "" Then
strInput = ActiveCell.Value
strReplace = ""
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = strPattern
End With
If regEx.test(strInput) Then
Set objMatches = regEx.Execute(strInput)
For i = 0 To objMatches.Count - 1
ActiveCell.Offset(cnt).Value = objMatches.Item(i)
cnt = cnt + 1
Next
End If
End If
End Sub
Function simpleCellRegex(StrIn As String) As Variant
Dim regEx As Object
Dim regMC As Object
Dim X
Dim strPattern As String
Dim lngCnt As Long
strPattern = "[A-Z]{1,3}[0-9]{2,4}"
Set regEx = CreateObject("vbscript.regexp")
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = strPattern
If .Test(StrIn) Then
Set regMC = .Execute(StrIn)
ReDim X(0 To regMC.Count - 1) As String
For lngCnt = 0 To UBound(X)
X(lngCnt) = regMC(lngCnt)
Next
simpleCellRegex = X
Else
simpleCellRegex = "Not matched"
End If
End With
End Function