为什么这个 python 字符串比较没有按预期工作
Why isn't this python string comparison working as expected
我在python中有如下比较:
"LC08_L1TP_215068_20151114_20170402_01_T1_B1"=="LC08*_B[1-7]"
哪个应该 return 正确但不是。有谁知道问题是什么,如何解决?
您需要使用正则表达式。这可以通过 re
模块来完成。在 Python 中,仅当每个字符都相同时字符串才相等,而不是模式匹配。我相信您要实现的目标是:
import re
print(re.match("LC08[^B]*_B[1-7]", "LC08_L1TP_215068_20151114_20170402_01_T1_B1") != None)
请看re
module.
import re
def main():
mystring = 'LC08_L1TP_215068_20151114_20170402_01_T1_B1'
m = re.search('^LC08_.*B\d{1,7}', mystring)
print(m != None)
if __name__ == '__main__':
main()
我在python中有如下比较:
"LC08_L1TP_215068_20151114_20170402_01_T1_B1"=="LC08*_B[1-7]"
哪个应该 return 正确但不是。有谁知道问题是什么,如何解决?
您需要使用正则表达式。这可以通过 re
模块来完成。在 Python 中,仅当每个字符都相同时字符串才相等,而不是模式匹配。我相信您要实现的目标是:
import re
print(re.match("LC08[^B]*_B[1-7]", "LC08_L1TP_215068_20151114_20170402_01_T1_B1") != None)
请看re
module.
import re
def main():
mystring = 'LC08_L1TP_215068_20151114_20170402_01_T1_B1'
m = re.search('^LC08_.*B\d{1,7}', mystring)
print(m != None)
if __name__ == '__main__':
main()