繁体   English   中英

从python字符串中提取子字符串

[英]Extract substring from a python string

我想在下面的 9 位数字之前提取字符串:

tmp = place1_128017000_gw_cl_mask.tif

输出应该是place1

我可以这样做: tmp.split('_')[0]但我也希望该解决方案适用于:

tmp = place1_place2_128017000_gw_cl_mask.tif结果将是: place1_place2

您可以假设该号码也将是 9 位数字

假设我们可以将您的问题表述为希望子字符串达到,但不包括后跟所有数字的下划线,我们可以尝试:

tmp = "place1_place2_128017000_gw_cl_mask.tif"
m = re.search(r'^([^_]+(?:_[^_]+)*)_\d+_', tmp)
print(m.group(1))  # place1_place2

使用正则表达式和正则表达式的前瞻功能,这是一个简单的解决方案:

tmp = "place1_place2_128017000_gw_cl_mask.tif"
m = re.search(r'.+(?=_\d{9}_)', tmp)
print(m.group())

结果:

place1_place2

请注意, \d{9}位正好匹配 9 个数字。 并且(?= ... )中的正则表达式位是前瞻,这意味着它不是实际匹配的一部分,但只有在匹配之后才匹配。

使用正则表达式:

import re

places = (
    "place1_128017000_gw_cl_mask.tif",
    "place1_place2_128017000_gw_cl_mask.tif",
)
pattern = re.compile("(place\d+(?:_place\d+)*)_\d{9}")
for p in places:
    matched = pattern.match(p)
    if matched:
        print(matched.group(1))

印刷:

地点1

地点1_地点2

正则表达式的工作方式如下(根据需要进行调整,例如,少于 9 位或可变位数):

  • (开始捕获
  • place\d+匹配“位置加 1 到多个数字”
  • (?:启动一个组,但不捕获它(无需捕获)
  • _place\d+匹配更多“地点”
  • )关闭组
  • *表示前一组的零次或多次
  • )关闭捕获
  • \d{9}匹配 9 位数字

结果在第一个(也是唯一的)捕获组中。

这是一个没有正则表达式的可能解决方案(未优化!):

def extract(s):
    result = ''
    for x in s.split('_'):
        try: x = int(x)
        except: pass
        if isinstance(x, int) and len(str(x)) == 9:
            return result[:-1]
        else:
            result += x + '_'

tmp = 'place1_128017000_gw_cl_mask.tif'
tmp2 = 'place1_place2_128017000_gw_cl_mask.tif'

print(extract(tmp))   # place1
print(extract(tmp2))  # place1_place2 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM