MS Excel - 从子字符串中提取独特的文本模式关键字
MS Excel - Extract Unique Text Patterns Keyword from Substring
我想通过使用 MS Excel 公式从包含唯一搜索模式的文本子字符串中提取 text/year。我有一列 (Cell A2:A19) 的唯一日期,在某些情况下,文本前后有周围的文本,我只想提取所有唯一以数字“1”开头的年份(即, 17??, 18??, 19??, 等等)
当前公式
DATE
April 1 1799
April 11 1867
February 12 1806
February 21 1798
February 28 1844 as Delaware Township
February 5 1798
February 7 1892 Verona Township
February 9 1797
January 19 1748
July 10 1721 as Upper Penns Neck Township
March 15 1860
March 17 1870 (as Raritan Township)
March 17 1874
March 23 1864
March 5 1867
April 28th 1828
1840s
1878 as Lehigh Township
当前结果(来自上面使用的公式)
理想的结果:
除了 1840s
之外的所有公式
=TRIM(MID(A2,MIN(IFERROR(SEARCH({"17?? ","18?? ","19?? "},A2 & " "),1E+99)),4))
这是一个数组公式,必须在退出编辑模式时用Ctrl-Shift-Enter确认。如果操作正确,那么 Excel 将在公式周围放置 {}
。
如果允许使用宏,您可以创建一个代码模块并将此代码放入其中:
Public Function GetYear(text As String)
Dim regex
Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = ".*(\d{4}).*"
Set matches = regex.Execute(text)
GetYear = matches(0).Submatches(0)
End Function
然后,您可以使用“=GetYear(A1)”这样的公式来获取所有年份,包括 1840 年代和其他类似格式。它基本上是在给定的字符串中搜索一组相邻的四位数字。不是最佳日期匹配算法,但听起来足以满足您的用例。
使用 Scott 提供的 MS Excel 公式,我做了一个小调整,解决了 capturing/extracting 只有日期和其他类似问题。这是通过仅在问号“??”之后添加通配符星号来表示任何字符系列来实现的。谢谢斯科特... =IFERROR(TRIM(MID(A2,MIN(IFERROR(SEARCH({"16??*","17??*","18??*","19?? * "},A2 & " "),1E+99)),4)),"")
我想通过使用 MS Excel 公式从包含唯一搜索模式的文本子字符串中提取 text/year。我有一列 (Cell A2:A19) 的唯一日期,在某些情况下,文本前后有周围的文本,我只想提取所有唯一以数字“1”开头的年份(即, 17??, 18??, 19??, 等等)
当前公式
DATE
April 1 1799
April 11 1867
February 12 1806
February 21 1798
February 28 1844 as Delaware Township
February 5 1798
February 7 1892 Verona Township
February 9 1797
January 19 1748
July 10 1721 as Upper Penns Neck Township
March 15 1860
March 17 1870 (as Raritan Township)
March 17 1874
March 23 1864
March 5 1867
April 28th 1828
1840s
1878 as Lehigh Township
当前结果(来自上面使用的公式)
理想的结果:
除了 1840s
=TRIM(MID(A2,MIN(IFERROR(SEARCH({"17?? ","18?? ","19?? "},A2 & " "),1E+99)),4))
这是一个数组公式,必须在退出编辑模式时用Ctrl-Shift-Enter确认。如果操作正确,那么 Excel 将在公式周围放置 {}
。
如果允许使用宏,您可以创建一个代码模块并将此代码放入其中:
Public Function GetYear(text As String)
Dim regex
Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = ".*(\d{4}).*"
Set matches = regex.Execute(text)
GetYear = matches(0).Submatches(0)
End Function
然后,您可以使用“=GetYear(A1)”这样的公式来获取所有年份,包括 1840 年代和其他类似格式。它基本上是在给定的字符串中搜索一组相邻的四位数字。不是最佳日期匹配算法,但听起来足以满足您的用例。
使用 Scott 提供的 MS Excel 公式,我做了一个小调整,解决了 capturing/extracting 只有日期和其他类似问题。这是通过仅在问号“??”之后添加通配符星号来表示任何字符系列来实现的。谢谢斯科特... =IFERROR(TRIM(MID(A2,MIN(IFERROR(SEARCH({"16??*","17??*","18??*","19?? * "},A2 & " "),1E+99)),4)),"")