我想用空格替换所有非字母字符,不包括1950年到2029年之间的年份。例如:
ab-c 0123 4r. a2017 2010
-> ab c r a 2010
到目前为止,我的尝试是通过负面预测将日期列入黑名单:
re.sub('(?!\b19[5-9][0-9]\b|\b20[0-2][0-9]\b)([^A-Za-z]+)', ' ', string)
由于这不起作用,任何帮助都是非常感谢的!
发布于 2017-09-07 16:00:40
您可以使用一个简单的正则表达式并传递一个函数来检查它是否是一年:
import re
def replace_non_year_numbers(m):
number = int(m.group(0))
if 1950 <= number <= 2029:
return str(number)
else:
return ''
print(re.sub('\d+', replace_non_year_numbers, 'ab-c 0123 4r. a2017 2010'))
# 'ab-c r. a2017 2010'
为了保持正则表达式和逻辑简单,您可以在第二步中删除特殊字符:
only_years = re.sub('\d+', replace_non_year_numbers, 'ab-c 0123 4r. a2017 2010')
no_special_char = re.sub('[^A-Za-z0-9 ]', ' ', only_years)
print(re.sub(' +', ' ', no_special_char))
# ab c r a2017 2010
发布于 2017-09-07 17:02:38
让我们选择您希望保留在结果中的内容。看看正则表达式:
(
(?<!\w) # neg. lookbehind: not a word char
(1 # read a '1'
(?=9[5-9][0-9]) # lookahead: following 3 digits make it
# a year between 1950 and 1999
[0-9]{3} # THEN read these 3 digits
| # - OR -
2 # read a '2'
(?=0[0-2][0-9]) # lookahead: following 3 digits make it
# a year between 2000 and 2029
[0-9]{3} # THEN read these 3 digits
)
| # - OR -
[a-zA-Z] # read some letter
)+
在一个线条中:
((?<!\w)(1(?=9[5-9][0-9])[0-9]{3}|2(?=0[0-2][0-9])[0-9]{3})|[a-zA-Z])+
您可以在regex 101上测试它
让我们把它放到一个python脚本中:
$ cat test.py
import re
pattern = r"(?:(?<!\w)(?:1(?=9[5-9][0-9])[0-9]{3}|2(?=0[0-2][0-9])[0-9]{3})|[a-zA-Z])+"
tests = ["ab-c 0123 4r. a2017 2010 a1955 1955 abc"]
for elt in tests:
matches = re.findall(pattern, elt)
print ' '.join(matches)
这就给出了:
$ python test.py
ab c r a 2010 a 1955 abc
发布于 2017-09-07 17:24:55
不是很漂亮,但我会使用多个替代:
import re
def check_if_year(m):
number = int(m.group(0))
if 1950 <= number <= 2029:
return str(number)
else:
return ' '
s = 'ab-c 0123 4r. a2017 2010 1800' # Added 1800 for testing
print(s)
print('ab c r a 2010')
t = re.sub(r'[^A-Za-z0-9 ]+', ' ', s) # Only non-alphanumeric
t = re.sub(r'(?!\b\d{4}\b)(?<!\d)\d+', ' ', t) # Only numbers that aren't standalone 4 digits
t = re.sub(r'\d+', check_if_year, t) # Only standalone 4 digits number and test for year
t = re.sub(r' {2,}', ' ', t).strip() # Clean up extra spaces
print(t)
(?!\b\d{4}\b)(?<!\d)\d+
将匹配任何数字,只要它不是一个独立的4位数字(除了空格或字符串开头/结尾之外没有其他字符),并且我使用(?<!\d)
,这样它就不会尝试在数字中间进行匹配。
https://stackoverflow.com/questions/46090928
复制相似问题