如何使用RegEx返回已解析的值? [英] How do you use RegEx to return a parsed value?

查看:152
本文介绍了如何使用RegEx返回已解析的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据列,具有多个级别的标题值,我只想要前三个级别,但是我不知道如何获得解析的值?

I have a data column that has a heading value with multiple levels, where I only want the first three levels, but I cannot figure out how to get the parsed value?

我正在阅读,并显示如何使用创建一个函数来返回一个布尔值,但是如何创建一个返回一个被解析的值的函数?

I was reading this and it shows how to use create a function to return a boolean for the condition, but how would I create a function that would return a parsed value?

这是我认为的正则表达式

This is the Regular Expression that I think I need.

^(\d.\d.\d)

我正在寻找可以将 1.2.3.4.5。更改为 1.2.3 和类似的任何其他标题我有三个以上的水平。

I'm looking for something that would change 1.2.3.4.5. to 1.2.3 and similar for any other header I have that has more than three levels.

理想情况下,我想可以把它放入我的查询设计作为一个字段表达式,但我不知道该怎么做。

Ideally, I'd like to be able to put it into my Query Design as a Field Expression, but I'm not sure how I would do that.

推荐答案

我假设您的输入值可能在点之间有多个数字。换句话说,我想你想要这个...

I assumed your input values could have more than one digit between the dots. In other words, I think you want this ...

? RegExpGetMatch("1.2.3.4.5.", "^(\d+\.\d+\.\d+).*", 1)
1.2.3
? RegExpGetMatch("1.27.3.4.5.", "^(\d+\.\d+\.\d+).*", 1)
1.27.3

如果这是正确的行为,这里是我使用的功能。

If that is the correct behavior, here is the function I used.

Public Function RegExpGetMatch(ByVal pSource As String, _
    ByVal pPattern As String, _
    ByVal pGroup As Long) As String

    'requires reference to Microsoft VBScript Regular Expressions
    'Dim re As RegExp
    'Set re = New RegExp
    'late binding; no reference needed
    Dim re As Object
    Set re = CreateObject("VBScript.RegExp")

    re.Global = True
    re.Pattern = pPattern
    RegExpGetMatch = re.Replace(pSource, "$" & pGroup)
    Set re = Nothing
End Function

另请参阅此答案,由 kazJaw 。他的答案告诉我如何使用 RegExp.Replace 选择匹配组。

See also this answer by KazJaw. His answer taught me how to select the match group with RegExp.Replace.

在Access中运行的查询会话,你可以使用这样的功能:

In a query run within an Access session, you could use the function like this:

SELECT
    RegExpGetMatch([Data Column], "^(\d+\.\d+\.\d+).*", 1) AS parsed_value
FROM YourTable;

请注意,自定义VBA函数不可用于从Access会话之外运行的查询。

Note however a custom VBA function is not usable for queries run from outside an Access session.

这篇关于如何使用RegEx返回已解析的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆