首页 > 解决方案 > RegEx 用于识别后跟特殊模式的日期

问题描述

我有一个以不同间隔出现的字符串/值模式。模式如下: 30/09/2016 2,085,669 0 0 UC 否

  1. 日期>空格>逗号分隔的数字>空格>数字>空格>数字>空格>字符串>空格>数字

我如何识别它并从细胞中提取。我一直在尝试使用正则表达式来解决这个问题。请注意,该模式可以在单个单元格中的任何实例中出现。即。

  1. Somestring(空格)(30/09/2016 2,085,669 0 0 UC No)(空格) 更多字符串
  2. Somemorestring(空格)(2016 年 9 月 30 日 2,085,669 0 0 UC 否)
  3. 括号仅用于说明

为了确定日期,我使用了下面的正则表达式,这不是最好的方法,但可以完成我的工作。

(^\d{1,2}\/\d{1,2}\/\d{4}$)

如何用剩余的图案缝合这个?

标签: regexvbaregex-lookaroundsregex-groupregex-greedy

解决方案


You are only matching the date like part between the anchors to assert the start ^ and the end $ of the string.

Note that if you only want to match the value you can omit the parenthesis () to make it a capturing group around the expression.

You could extend it to:

^\d{1,2}\/\d{1,2}\/\d{4} \d+(?:,\d+)+ \d+ \d+ [A-Za-z]+ [A-Za-z]+$

Explanation

  • ^ Start of string
  • \d{1,2}\/\d{1,2}\/\d{4} Match date like pattern
  • \d+(?:,\d+)+ Match 1+ digits and repeat 1+ times matching a comma and a digit
  • \d+ \d+ Match two times 1+ digits followed by a space
  • [A-Za-z]+ [A-Za-z]+ Match 2 times 1+ chars a-z followed by a space
  • $ End of string

Regex demo


推荐阅读