正则表达式是否匹配引号之外的所有单词? [英] Regex match all the words outside quotation marks?

查看:240
本文介绍了正则表达式是否匹配引号之外的所有单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过使用标准正则表达式解析器来匹配引号外的文本? 我已经看到了这个答案,但这是通过使用PCRE来完成的:

Is it possible to match text outside quotation marks by using standard regex parser? I have seen this answer, but it is done by using PCRE:

正则表达式可以匹配所有外部单词引号?

由于使用PERL,这不是一个纯粹的解决方案.我知道也可以使用编程语言来解决它,但是这个想法是使用纯正则表达式解析器.

This is not a pure solution because of using PERL. I know that it also can be solved by using programming language, but the idea is to use pure regex parser.

我做了类似的事情,但这不能正常工作

I have made something like this, but this is not working correctly

[^'"]*(?=(?:(['"])+(.*?\1))|([^'"]*$))

谢谢.

UPD1 :其想法是匹配引号之外的任何类型的文本,解决方案不得依赖于输入.

UPD1:The idea is to match any kind of text outside quotation marks, the solution must not depend on the input.

推荐答案

我想出了以下解决方案:

I came up with this solution:

(?:[^"](?=(?:[^"]*?(?:["][^"]*?["][^"]*?)+$)|(?:[^"]*?$)))*|(^[^"]*["][^"]*$)

http://regex101.com/r/pI8xA4/2

如果引号数量奇数,它将不能很好地工作-在这种情况下,它将跳过第一个引号. 但这是目前对我来说最好的解决方案.

it will not work very well if we have an odd number of quotes - In this case, it will skip the first quote. But it is the best solution for me for now.

这篇关于正则表达式是否匹配引号之外的所有单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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