假设我有一个字符串'gfgfdAAA1234ZZZuijjk',我想提取'1234'部分。
我只知道在AAA之前的几个字符,以及在ZZZ之后的我感兴趣的部分1234。
使用sed,可以对字符串执行如下操作:
echo "$STRING" | sed -e "s|.*AAA\(.*\)ZZZ.*|\1|"
结果是1234。
如何在Python中做同样的事情?
假设我有一个字符串'gfgfdAAA1234ZZZuijjk',我想提取'1234'部分。
我只知道在AAA之前的几个字符,以及在ZZZ之后的我感兴趣的部分1234。
使用sed,可以对字符串执行如下操作:
echo "$STRING" | sed -e "s|.*AAA\(.*\)ZZZ.*|\1|"
结果是1234。
如何在Python中做同样的事情?
当前回答
使用sed,可以对字符串执行如下操作:
echo "$STRING" | sed -e "s|.*AAA\(.*\)ZZZ.*|\1|"
结果是1234。
你可以使用相同的正则表达式对re.sub函数做同样的事情。
>>> re.sub(r'.*AAA(.*)ZZZ.*', r'\1', 'gfgfdAAA1234ZZZuijjk')
'1234'
在基本sed中,捕获组由\(..\)表示,但在python中由(..)表示。
其他回答
>>> s = 'gfgfdAAA1234ZZZuijjk'
>>> start = s.find('AAA') + 3
>>> end = s.find('ZZZ', start)
>>> s[start:end]
'1234'
然后,如果您愿意,也可以在re模块中使用regexp,但在您的情况下这不是必要的。
另一种方法是使用列表(假设你正在寻找的子字符串是由数字组成的,只是):
string = 'gfgfdAAA1234ZZZuijjk'
numbersList = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
output = []
for char in string:
if char in numbersList: output.append(char)
print(f"output: {''.join(output)}")
### output: 1234
此外,您可以在波纹函数中找到所有的组合
s = 'Part 1. Part 2. Part 3 then more text'
def find_all_places(text,word):
word_places = []
i=0
while True:
word_place = text.find(word,i)
i+=len(word)+word_place
if i>=len(text):
break
if word_place<0:
break
word_places.append(word_place)
return word_places
def find_all_combination(text,start,end):
start_places = find_all_places(text,start)
end_places = find_all_places(text,end)
combination_list = []
for start_place in start_places:
for end_place in end_places:
print(start_place)
print(end_place)
if start_place>=end_place:
continue
combination_list.append(text[start_place:end_place])
return combination_list
find_all_combination(s,"Part","Part")
结果:
['Part 1. ', 'Part 1. Part 2. ', 'Part 2. ']
使用PyParsing
import pyparsing as pp
word = pp.Word(pp.alphanums)
s = 'gfgfdAAA1234ZZZuijjk'
rule = pp.nestedExpr('AAA', 'ZZZ')
for match in rule.searchString(s):
print(match)
收益率:
[[1234]]
只需一行代码就可以做到
>>> import re
>>> re.findall(r'\d{1,5}','gfgfdAAA1234ZZZuijjk')
>>> ['1234']
结果将收到列表…