正则表达式仅返回七位数的数字 [英] Regex return seven digit number match only

查看:155
本文介绍了正则表达式仅返回七位数的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试构建正则表达式以从字符串中提取7位数字,但是很难正确设置模式.

I've been trying to build a regular expression to extract a 7 digit number from a string but having difficulty getting the pattern correct.

示例字符串-WO1519641 WO1528113TB WO1530212 TB

示例返回-1519641, 1528113, 1530212

我在Excel中使用的代码是...

My code I'm using in Excel is...

Private Sub Extract7Digits()
    Dim regEx As New RegExp
    Dim strPattern As String
    Dim strInput As String
    Dim strReplace As String
    Dim Myrange As Range

    Set Myrange = ActiveSheet.Range("A1:A300")

    For Each c In Myrange
        strPattern = "\D(\d{7})\D"
        'strPattern = "(?:\D)(\d{7})(?:\D)"
        'strPattern = "(\d{7}(\D\d{7}\D))"

        strInput = c.Value

        With regEx
            .Global = True
            .MultiLine = True
            .IgnoreCase = False
            .Pattern = strPattern
        End With

        If regEx.test(strInput) Then
            Set matches = regEx.Execute(strInput)
            For Each Match In matches
                s = s & " Word: " & Match.Value & " "
            Next
                c.Offset(0, 1) = s
            Else
                s = ""
        End If

    Next
End Sub

我已经尝试了该代码中的所有3种模式,但最终在使用"\D(\d{7})\D"时得到了O1519641, O1528113T, O1530212的返回.据我所知,()并没有什么意义,因为我存储匹配项的方式很早,但我最初认为它们意味着表达式会返回 ()内部的内容.

I've tried all 3 patterns in that code but I end up getting a return of O1519641, O1528113T, O1530212 when using "\D(\d{7})\D". As I understand now the () doesn't mean anything because of the way I am storing the matches while I initially thought they meant that the expression would return what was inside the ().

我一直在 http://regexr.com/上进行测试,但是我仍然不确定如何获取它以允许数字位于字符串中,如WO1528113TB,但仅返回数字.我是否需要对RegEx的返回值运行RegEx以第二次排除字母?

I've been testing things on http://regexr.com/ but I'm still unsure of how to get it to allow the number to be inside the string as WO1528113TB is but only return the numbers. Do I need to run a RegEx on the returned value of the RegEx to exclude the letters the second time around?

推荐答案

我建议使用以下模式:

strPattern = "(?:^|\D)(\d{7})(?!\d)"

然后,您将可以通过match.SubMatches(0)访问捕获组1的内容(即,用正则表达式的(\d{7})部分捕获的文本),然后可以检查哪个值最大.

Then, you will be able to access capturing group #1 contents (i.e. the text captured with the (\d{7}) part of the regex) via match.SubMatches(0), and then you may check which value is the largest.

模式详细信息:

  • (?:^|\D)-与字符串(^)或非数字(\D)开头匹配的非捕获组(不创建任何子匹配项)
  • (\d{7})-捕获与7位数字匹配的第1组
  • (?!\d)-如果在7位数字之后紧接着有一位数字,则负前行匹配将失败.
  • (?:^|\D) - a non-capturing group (does not create any submatch) matching the start of string (^) or a non-digit (\D)
  • (\d{7}) - Capturing group 1 matching 7 digits
  • (?!\d) - a negative lookahead failing the match if there is a digit immediately after the 7 digits.

这篇关于正则表达式仅返回七位数的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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