RegEx:在引号之间抓取值 [英] RegEx: Grabbing values between quotation marks

查看:50
本文介绍了RegEx:在引号之间抓取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的值:

"Foo Bar" "Another Value" something else

什么正则表达式会返回用引号括起来的值(例如 Foo BarAnother 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆