假设我有一个字符串
str='hello this is "vijay_kapoor" and welcome '
现在我只想过滤双引号" "
中的单词,因此输出为vijay
正则表达式是什么?
我尝试过:
re.search('"[a-zA-Z0-9_]*"', str').group()
但是没有用。
答案 0 :(得分:1)
import org.quartz.*;
@PersistJobDataAfterExecution
public class ExampleJob {
static triggers = {
simple repeatInterval: 1000l // execute job every 1 second
}
@Override
void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
JobDataMap jobDataMap = jobExecutionContext.jobDetails.jobDataMap
Integer count = jobDataMap.get("count") ?: 0
jobDataMap.put("count", ++count)
}
}
应该可以正常工作:
\"(.+?)\"
输出:
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])
答案 1 :(得分:1)
初学者不要灰心。该代码应该在这里工作。
import re
string='hello this is "vijay_kapoor" and welcome '
regex = re.compile(r'\x22(\w+)_')
match = regex.search(string)
print(match.group(1))
答案 2 :(得分:0)
您可以尝试
re.search('"[a-z]+_',str).group()[1:-1]