正则表达式否定不起作用 Python
regular expressions Negation not working Python
所以我试图否认这种模式,但它不起作用,即使我用 () 将它包裹起来。我假设它与锚点混淆了,但我找不到绕过这个问题的方法。我检查了其他问题,但没有找到针对我的特定问题的解决方案:/
我们的想法是只获取不匹配 latitude/longitude 数字序列的案例。
[i for i in [re.findall(r"^\-?[0-9]+\.[0-9]+", string) for string in real_state['latitude']]]
data
我建议用你的模式拆分字符串:
import re
s = "Text: 0.12345 and -12.34433 and more to come"
results = re.split(r"\s*-?[0-9]+\.[0-9]+\s*", s)
print(results)
参见Python demo。
如果出现任何空项,例如匹配项出现在字符串的 start/end 处,请使用 filter
:
删除它们
import re
s = "0.12345 and -12.34433 and more to come 0.54321 and -27.87654"
results = re.split(r"\s*-?[0-9]+\.[0-9]+\s*", s)
# print(results) # => ['', 'and', 'and more to come', 'and', '']
print(list(filter(None, results))) # => ['and', 'and more to come', 'and']
所以我试图否认这种模式,但它不起作用,即使我用 () 将它包裹起来。我假设它与锚点混淆了,但我找不到绕过这个问题的方法。我检查了其他问题,但没有找到针对我的特定问题的解决方案:/
我们的想法是只获取不匹配 latitude/longitude 数字序列的案例。
[i for i in [re.findall(r"^\-?[0-9]+\.[0-9]+", string) for string in real_state['latitude']]]
data
我建议用你的模式拆分字符串:
import re
s = "Text: 0.12345 and -12.34433 and more to come"
results = re.split(r"\s*-?[0-9]+\.[0-9]+\s*", s)
print(results)
参见Python demo。
如果出现任何空项,例如匹配项出现在字符串的 start/end 处,请使用 filter
:
import re
s = "0.12345 and -12.34433 and more to come 0.54321 and -27.87654"
results = re.split(r"\s*-?[0-9]+\.[0-9]+\s*", s)
# print(results) # => ['', 'and', 'and more to come', 'and', '']
print(list(filter(None, results))) # => ['and', 'and more to come', 'and']