正则表达式是什么,以匹配一个字符串与两个下划线分解3系列字符。如果可能的话,这也可以是SQL'like'语句。
匹配度:
b_06/18/2012_06:02:34 PM
y1289423_06/14/2011_03:06:35 AM
23479693_11/01/2011_06:12:55 PM
不匹配:
CCC Valuation_b_06/28/2012_05:57:20 PM
CCC Valuation_CCC Valuation_b_06/28/2012_05:57:20 PM
doc1_2.pdf
testdoc.txt
答案 0 :(得分:3)
如果所有数据都与您的示例一致,那么这应该有效:
编辑:已更新以匹配整行。这将排除无效列表中的任何子字符串匹配。但是,它假定OP不想匹配任何子字符串。
^[a-z0-9]+_[0-9\/]+_[A-Z0-9:\s]+$
例如,在python中:
>>> import re
>>> s = 'b_06/18/2012_06:02:34 PM'
>>> pattern = '^[a-z0-9]+_[0-9\/]+_[A-Z0-9:\s]+$'
>>> m = re.match(pattern, s)
>>> m.group(0)
'b_06/18/2012_06:02:34 PM' # <======== matches from valid list
>>> s = 'CCC Valuation_CCC Valuation_b_06/28/2012_05:57:20 PM'
>>> m = re.match(pattern, s)
>>> m.group(0)
Traceback (most recent call last): # <======= does NOT match from invalid list
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
答案 1 :(得分:1)
^[^_]+_[0-9\/]+_[0-9:]+\s[AM|PM]