繁体   English   中英

尝试使用正则表达式在python中的双引号内查找模式

[英]trying to find patterns within double quotes in python using regex

假设我有一个字符串

str='hello this is "vijay_kapoor" and welcome ' 

现在我只想过滤双引号" "的单词,所以输出是vijay什么是正则表达式?

我试过了:

re.search('"[a-zA-Z0-9_]*"', str').group()

但它没有用。

\\"(.+?)\\"应该可以正常工作:

import re
def double_quotes(text):
  matches=re.findall(r'\"(.+?)\"',text)
  return ", ".join(matches)

print(double_quotes('hello this is "vijay_kapoor" and welcome'))

输出

vijay_kapoor

编辑

如果目的是在_之前进一步获得名称,则可以拆分它:

print(double_quotes('hello this is "vijay_kapoor" and welcome, ').split('_', 1)[0])

输出

vijay

作为初学者不要沮丧。 代码应该在这里工作。

import re
string='hello this is "vijay_kapoor" and welcome '
regex = re.compile(r'\x22(\w+)_')
match = regex.search(string)
print(match.group(1))

Python演示

你可以试试这个

re.search('"[a-z]+_',str).group()[1:-1]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM