当一个句子中有多对引号的时候,例如:
Last "week" I "went" to the "theatre"
正则表达式"[^"]+"只能匹配最后一个引号里的内容:theatre
我想匹配第二个引号里的内容( went ),该怎么写规则呢?
当一个句子中有多对引号的时候,例如:
Last "week" I "went" to the "theatre"
正则表达式"[^"]+"只能匹配最后一个引号里的内容:theatre
我想匹配第二个引号里的内容( went ),该怎么写规则呢?
|
chouryJun 20, 2019 via Android
1
多匹配点,然后别的不捕获
|
|
mooncakejsJun 20, 2019
2
'Last "week" I "went" to the "theatre"'.match(/"\w+"/g)
|
|
3
@mooncakejs 匹配不到
|
|
clarkyiJun 20, 2019
4
"\w+"$
|
|
mooncakejsJun 20, 2019
6
@guxianbang 这是全匹配出来,找最后一个就好了。
只要最后一个就 'Last "week" I "went" to the "theatre"'.match(/"\w+"$/) |
|
7
|
|
loryyangJun 20, 2019
8
re.match(r'[^"]*"[^"]*"[^"]*"([^"]*)".*', 'Last "week" I "went" to the "theatre"').group(1)
得到 went |
|
9
@guxianbang 我觉得不是正则的问题,是你获取的不对
|
|
10
谢谢各位🙏,我研究一下
|
|
pkookp8Jun 20, 2019 via Android
11
^.*?".*?".*?"(.*?)"
\1 |
|
ahaodadyJun 20, 2019
12
解题问问,突然想到的一个问题
|
|
ahaodadyJun 20, 2019
13
12345
使用 \d{2} 只能取到 12、34 我怎么取 23、45 呢 |
|
ahaodadyJun 20, 2019
14
顺便回答题主,你的正则会取出所有引号里的内容,如果确定是最后一项的话,在代码里取最后一条即可。
如果是编辑器里的话,结尾带个$ "([^"]+)"$ |
|
azh7138mJun 20, 2019
15
零宽断言?
^.*?(?<=".*?").*?"(.*?)" 先匹配一个"",然后取后面这个"" |
|
zhzbqlJun 20, 2019
16
js 解法
/^.*?".*?".*?"(.*?)"/.exec('Last "week" I "went" to the "theatre"')[1] 或 'Last "week" I "went" to the "theatre"'.match(/".*?"/g)[1] |
|
hjq98765Jun 20, 2019
17
|
|
ismyyymJun 20, 2019
18
python:
import re str='Last "week" I "went" to the "theatre"' x=re.search('^.*?".*?".*?"(.*?)"',str ).group(1) #正数第二个”“ y=re.search('.*"(.*?)".*?".*?".*?$',str ).group(1) #倒数第二个”“ z=re.findall('"(.*?)"',str)[1] # 返回所有”“,取第二个 用对下面这些 ^ $ *? 开头 结尾 尽可能少 |