未包含在尖括号中的文本的正则表达式-python
我需要得到的文本,是不是在角括号内 我的输入如下所示:未包含在尖括号中的文本的正则表达式-python,python,regex,string,brackets,Python,Regex,String,Brackets,我需要得到的文本,是不是在角括号内 我的输入如下所示: > whatever something<X="Y" zzz="abc">this is a foo bar <this is a > < whatever>and i ><only want this >随便什么这是一个foo-bar和i>随便什么 这是一家美食酒吧 在正则表达式模式中,应该将括号移到引号内(并删除已有的括号),以获取之间的所有文本,包括括号本身。您还需要排除\n字符
> whatever something<X="Y" zzz="abc">this is a foo bar <this is a
> < whatever>and i ><only want this
>随便什么这是一个foo-bar和i>随便什么
这是一家美食酒吧
在正则表达式模式中,应该将括号移到引号内(并删除已有的括号),以获取
之间的所有文本,包括括号本身。您还需要排除\n
字符以获得所需的输出
import re
x = """whatever something<X="Y" zzz="abc">this is a foo bar <this is a\n\
< whatever>and i ><only want this"""
y = re.findall("(<[^>\n]*>)",x.strip())
z = x[:]
for i in y:
z = z.replace(i,'\n')
print(z)
whatever something
this is a foo bar <this is a
and i ><only want this
重新导入
x=“”这是一个foo-bar和i>)的任何东西“,x.strip())
z=x[:]
对于y中的i:
z=z。替换(i,“\n”)
打印(z)
随便什么
这是一个foo-bar为什么没有从输出中删除
?我不使用python,但是这个
匹配您需要在示例数据中替换的所有内容。这样行吗?不过,嗅探器是有道理的。我只是想“删除你不想要的东西”,因为它是
=)。我假设没有任何标签中包含\n
。啊,它遗漏了第三个输出行上的
?我不理解您的问题。。。此位的输出正是您在原始问题中输入的所需输出。。。你想把
放在i
之后吗?在
之前有一个
和一个i>
。不过别担心,我也抓到了;}
import re
x = """whatever something<X="Y" zzz="abc">this is a foo bar <this is a\n< whatever>and i ><only want this"""
re.findall("<([^>]*)>", x.strip())
['X="Y" zzz="abc"', 'this is a\n ', ' whatever']
import re
x = """whatever something<X="Y" zzz="abc">this is a foo bar <this is a\n\
< whatever>and i ><only want this"""
y = re.findall("(<[^>\n]*>)",x.strip())
z = x[:]
for i in y:
z = z.replace(i,'\n')
print(z)
whatever something
this is a foo bar <this is a
and i ><only want this