RegEx:在引号之间抓取值 [英] RegEx: Grabbing values between quotation marks
问题描述
我有一个这样的值:
"Foo Bar" "Another Value" something else
什么正则表达式会返回用引号括起来的值(例如 Foo Bar
和 Another Value
)?
What regex will return the values enclosed in the quotation marks (e.g. Foo Bar
and Another Value
)?
推荐答案
我一直在使用以下方法并取得了巨大的成功:
I've been using the following with great success:
(["'])(?:(?=(\\?))\2.)*?\1
它还支持嵌套引号.
对于那些想要更深入地解释其工作原理的人,以下是用户 ephemient 的解释:
For those who want a deeper explanation of how this works, here's an explanation from user ephemient:
([""'])
匹配一个引号;((?=(\\?))\2.)
如果反斜杠存在,吞噬它,无论是否发生,匹配一个字符;*?
匹配多次(非贪婪,以免吃掉结束语);\1
匹配用于打开的同一个引号.
([""'])
match a quote;((?=(\\?))\2.)
if backslash exists, gobble it, and whether or not that happens, match a character;*?
match many times (non-greedily, as to not eat the closing quote);\1
match the same quote that was use for opening.
这篇关于RegEx:在引号之间抓取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!