子字符串中的字符串

时间:2018-05-19 03:16:53

标签: python python-3.x

我在列表中有一个字符串:

myData=["GndWork0920932jdjs-----#2.35 InherenowanotheeStemWork83kkjkfjks-----#4.55"]

我想首先找到'Gnd'然后得到值'2.35'然后我想找到Stem并获得值'4.55'。

1 个答案:

答案 0 :(得分:2)

这个答案可能没有用,但如果出于某种原因,有一个真实世界的应用程序有这样的任意数据格式,并且要考虑的案例数量是有限的,那么也许

import re

myData=["GndWork0920932jdjs-----#2.35 InherenowanotheeStemWork83kkjkfjks-----#4.55"]
regex = re.compile(".*(Gnd|Stem).*-----#(.+)")

for s in myData[0].split():
  m = regex.match(s)
  print(m.group(1), m.group(2))