excel - 提取多个表达式
问题描述
我有一个单元格,其中包含分配给这样的项目的用户名
,FC757_random_name,AP372_another_one,FC782_again_different,FC082_samesamebutdifferent,
我只需要提取表达式开头的字母数字值,因此介于,
和之间的所有内容_
。
我用以下方法使它适用于一个表达式,但我需要所有这些。
= MID(A1;FIND(",";A1)+1;FIND("_";A1)-FIND(",";A1)-1)
我还修改了 Text to Data,但无法使其同时适用于多行。理想情况下,这只适用于公式,但我想(/恐惧)我需要 VBA 或宏,这是我以前从未使用过的。
所有帮助将不胜感激!
解决方案
这是一个基于正则表达式的用户定义函数。
Option Explicit
Function extractMultipleExpressions(str As String, _
Optional delim As String = ", ")
Dim n As Long, nums() As Variant
Static rgx As Object, cmat As Object
'with rgx as static, it only has to be created once; beneficial when filling a long column with this UDF
If rgx Is Nothing Then
Set rgx = CreateObject("VBScript.RegExp")
End If
extractMultipleExpressions = vbNullString
With rgx
.Global = True
.MultiLine = False
.Pattern = "[A-Z]{2}[0-9]{3}"
If .Test(str) Then
Set cmat = .Execute(str)
'resize the nums array to accept the matches
ReDim nums(cmat.Count - 1)
'populate the nums array with the matches
For n = LBound(nums) To UBound(nums)
nums(n) = cmat.Item(n)
Next n
'convert the nums array to a delimited string
extractMultipleExpressions = Join(nums, delim)
End If
End With
End Function
推荐阅读
- xaml - UWP 用户控件在 Xaml 岛中工作是否存在某种代码大小限制?(退出代码 0xc000027b)
- amazon-web-services - AWS Glue - 将列更改为类型数组或结构
- apify - injectFile 方法的survivalNavigations 选项的问题
- javascript - 发出需要打印的传递值
- postgresql - 欧洲废物目录代码的父/树参照完整性检查
- html - 如何使用 CSS 在 Ionic ion-segment-button 中使图标和文本彼此并排?
- sql - 将 JSON 数据提取到简单的列中
- java - 为什么服务器不响应 GET 请求
- laravel - Laravel Bootstrap 脚手架到 VUE 脚手架 - 如何?
- azure - 为什么 AKS 使用比 Azure 上的 Web 应用服务更多的资源