代码之家  ›  专栏  ›  技术社区  ›  Marcin

在Python正则表达式中从字符串中提取子字符串

  •  0
  • Marcin  · 技术社区  · 8 月前

    假设我有一个长字符串,其地址数据格式为:

    string = ... 'Something 3; Place: Something; City: NewYork; Street: Broadway; House: 7; Lat g: 33,105438; Long g: 12,749558; Object: Something;'...

    我想从以下字符串中提取子字符串 Place: Long g: 12,749558. 使用正则表达式findall()。 有人能帮帮我吗?谢谢!

    我想从我的字符串中获取:

    Place: Something; City: NewYork; Street: Broadway; House: 7; Lat g: 33,105438; Long g: 12,749558;

    1 回复  |  直到 8 月前
        1
  •  0
  •   nk0    8 月前

    您可以使用以下正则表达式模式: r'Place:.*?Long g: \d+,\d+;' .

    代码示例:

    import re
    
    string = '... Something 3;  Place: Something; City: NewYork; Street: Broadway; House: 7; Lat g:  33,105438; Long g: 12,749558; Object: Something;...'
    pattern = r'Place:.*?Long g: \d+,\d+;'
    result = re.findall(pattern, string) # "Place: Something; City: NewYork; Street: Broadway; House: 7; Lat g:  33,105438; Long g: 12,749558;"