Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 仅适用于五个连续数字的正则表达式模式_Regex_Excel_Vba - Fatal编程技术网

Regex 仅适用于五个连续数字的正则表达式模式

Regex 仅适用于五个连续数字的正则表达式模式,regex,excel,vba,Regex,Excel,Vba,我只需要一个模式来提取五个连续的数字 示例A:123456789 dsadss12345@3-1d22d333 示例B:12345\u2d2D2AAA2222A 示例C:2D2JJDDD_u@12345 想要的输出:12345 或 (非数字:“电子空间”;“字母”;“符号:,-等)(5个连续数字)(非数字:“电子空间”;“字母”;“符号:,-等) 或 (5个连续数字)(不是数字:“电子空间”、“字母”、“符号:、-&等”) 或 (不是数字:“espace”;“字母”;“符号:,-&等”)(5个连

我只需要一个模式来提取五个连续的数字

示例A:123456789 dsadss12345@3-1d22d333

示例B:12345\u2d2D2AAA2222A

示例C:2D2JJDDD_u@12345

想要的输出:12345

(非数字:“电子空间”;“字母”;“符号:,-等)(5个连续数字)(非数字:“电子空间”;“字母”;“符号:,-等)

(5个连续数字)(不是数字:“电子空间”、“字母”、“符号:、-&等”)

(不是数字:“espace”;“字母”;“符号:,-&等”)(5个连续数字)

尝试了以下方法:

  (1)strPattern = "(((\s|\D|\W)(\d{5})(\s|\D|\W))|((\d{5})(\s|\D|\W))|((\s|\D|\W)(\d{5})))"
  (2)strPattern = "\d{5}"
  (3)strPattern = "(\s|\D|\W)(\d{5})(\s|\D|\W)"
  (4)strPattern = "\D(\d{5})\D"
(3) 不适用于示例B

(2) 不适用于示例A

(1) 是我能得到的最好的,但它在少数单元格中不起作用。

您可以使用

(?:^|\D)(\d{5})(?!\d)

(?:^ |\D)
将匹配字符串的开头或数字以外的任何字符,
(\D{5})
将捕获5个数字到组1中,
(?!\D)
将确保下一个字符不是数字

示例代码:

Dim re, targetString, colMatch, objMatch
Set re = New regexp
With re
 .pattern = "(?:^|\D)(\d{5})(?!\d)"
 .Global = True              ' Same as /g at the online tester
End With
targetString = "123456789 dsadss12345@3_- 1d22d333"
Set colMatch = re.Execute(targetString)
For Each objMatch In colMatch
  Debug.Print objMatch.SubMatches.Item(0)
Next
正则表达式备忘单 这本身并不是一个真正的答案,但我认为这个优秀的总结是值得的(在这里更容易分享!)

单击图像查看全尺寸。

这通过了OP:

((?<!\d)\d{5}(?!\d))
(?
完全匹配将只有5个连续的数字,周围除了一个数字以外的任何东西

证据就在布丁里:


示例C中的下一个字符是什么?示例B中的上一个字符是什么?为了简化,每个示例都需要一个清晰的开始/停止。您可以使用并抓取
匹配.submatch(0)
(\d{5}?)
是正好5个数字的模式。在示例B中没有字符,单元格以12345开头,在示例C中以12345结尾,this(?:^ |\D)(\D{5})(?!\D)非常好,谢谢!有没有一种不用物体就能使用SubMatch的方法?@Wiktor Stribiżew@ehdgur对不起,我不知道你的意思。
.SubMatches
Match
对象的一个属性。我花了几年时间才弄明白regex…但它实际上非常简单。将示例文本粘贴到t combines.proof它起作用:…(而且,它与前面的答案相同!)不完全相同,因为我们提取了不同的匹配项!但是对于coolOPs full match的链接,tyvm返回字符串char mine是100%直接向上的连续数字