在Python 3.7(在Windows 64位上测试),使用RegEx替换字符串.*会使输入字符串重复两次!
在Python 3.7.2上:
>>> import re
>>> re.sub(".*", "(replacement)", "sample text")
'(replacement)(replacement)'
Run Code Online (Sandbox Code Playgroud)
在Python 3.6.4上:
>>> import re
>>> re.sub(".*", "(replacement)", "sample text")
'(replacement)'
Run Code Online (Sandbox Code Playgroud)
在Python 2.7.5(32位)上:
>>> import re
>>> re.sub(".*", "(replacement)", "sample text")
'(replacement)'
Run Code Online (Sandbox Code Playgroud)
怎么了?如何解决?
我正在尝试提取出该-字符串之前和之后的其余数字,但无法同时提取出两者。这是交互式终端的输出:
>>> a = '#232 - Hello There'
>>> re.findall('#(.*?) - (.*?)', a)
[('232', '')]
Run Code Online (Sandbox Code Playgroud)
为什么我的正则表达式无法正常工作?
我必须使用正则表达式从字符串中识别出不同的日期格式,如下所示。
date can contain 21/12/2018
or 12/21/2018
or 2018/12/21
or 12/2018
or 21-12-2018
or 12-21-2018
or 2018-12-21
or 21-Jan-2018
or Jan 21,2018
or 21st Jan 2018
or 21-Jan-2018
or Jan 21,2018
or 21st Jan 2018
or Jan 21, 2018
or Jan 21, 2018
or 2018 Dec. 21
or 2018 Dec 21
or 21st of Jan 2018
or 21st of Jan 2018
or Jan 2018
or Jan 2018
or Jan. 2018
or Jan, 2018
or 2018
[should recognize (year only), (year …Run Code Online (Sandbox Code Playgroud)