[英]Regular expression start and end with
With python I want to use regular expression(or any other way) to find all strings between使用 python 我想使用正则表达式(或任何其他方式)来查找之间的所有字符串
£ and £
$ and $
[ and ]
sample_text = "All £blocker£ §YTile Blockers§! have been cleared from $country$ $status$ [From.From.GetName]."
With this sample text I want to get output of有了这个示例文本,我想得到 output
blocker
country
status
From.From.GetName
Use re.findall
:使用
re.findall
:
# -*- coding: utf-8 -*-
import re
sample_text = "All £blocker£ §YTile Blockers§! have been cleared from $country$ $status$ [From.From.GetName]."
matches = re.findall(r'[£§$\[](.*?)[£§$\]]', sample_text)
print(matches) # ['blocker', 'YTile Blockers', 'country', 'status', 'From.From.GetName']
The regex pattern used here says to match:此处使用的正则表达式模式表示匹配:
[£§$\[]
match an opening marker [£§$\[]
匹配一个开始标记(.*?)
match and capture in \1
the content (.*?)
在\1
中匹配并捕获内容[£§$\]]
then match a closing marker [£§$\]]
然后匹配一个结束标记([£§$])(\S*)\1
can exclude spaces. ([£§$])(\S*)\1
可以排除空格。
Here is a solution without regular expression.这是一个没有正则表达式的解决方案。 It requires to implement a simple parsing function, apply it to the tokenized text and finally filter the results to exclude the None values.
它需要实现一个简单的解析 function,将其应用于标记化文本,最后过滤结果以排除 None 值。
def parse(word):
symbols = ['£', '$', '[]']
for s in symbols:
_len = len(s)
if _len == 1:
start = word.find(s)
end = word[start+1:].find(s)
if start != -1 and end != -1:
return word[start+1:start+end+1]
elif _len == 2:
start = word.find(s[0])
end = word[start+1:].find(s[-1])
if start != -1 and end != -1:
return word[start+1:start+end+1]
sample_text = "All £blocker£ §YTile Blockers§! have been cleared from $country$ $status$ [From.From.GetName]."
result = list(filter(None, map(parse, sample_text.split(' '))))
print(result) #['blocker', 'country', 'status', 'From.From.GetName']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.