使用 RegExp (PREG) 提取函数参数
考虑以下函数参数(它们已经从函数中提取):
Monkey,"Blue Monkey", "Red, blue and \"Green'", 'Red, blue and "Green\''
是否有一种方法可以提取参数以使用正则表达式并剥离空格来获取以下数组输出:
[Monkey, "Blue Monkey", "Red, blue and \"Green'", 'Red, blue and "Green\'']
我被困在使用这个正则表达式,但它不够宽松:
/(("[^"]+"|[^\s,]+))/g
Consider the following function arguments (they are already extracted of the function):
Monkey,"Blue Monkey", "Red, blue and \"Green'", 'Red, blue and "Green\''
Is there a way to extract arguments to get the following array ouput using regexp and stripping white spaces:
[Monkey, "Blue Monkey", "Red, blue and \"Green'", 'Red, blue and "Green\'']
I'm stuck using this RegExp which is not permisive enough:
/(("[^"]+"|[^\s,]+))/g
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这看起来有点令人讨厌,但它有效:
我使用
\x5C
而不是普通的反斜杠字符\
,因为太多可能会造成混淆。该正则表达式由以下部分组成:
"(?:[^\x5C"]+|\x5C(?:\x5C\x5C)*[\x5C"])*"
匹配双引号字符串声明'(?:[^\x5C']+|\x5C(?:\x5C\x5C)*[\x5C'])*'
匹配单引号字符串声明[^" ',]+
匹配"(?:[^\x5C"]+|\x5C(?:\x5C\x5C)*[\x5C"] 的部分 。 )*"
是:[^\x5C"]+
匹配除退格和引号字符之外的任何内容\x5C(?:\x5C\x5C)*[\x5C"]
匹配正确的转义序列,如\"
、\\
、\\\"
、\\\\等
This looks a little nasty but it works:
I used
\x5C
instead of the plain backslash character\
as too much of those can be confusing.This regular expression consists of the parts:
"(?:[^\x5C"]+|\x5C(?:\x5C\x5C)*[\x5C"])*"
matches double quoted string declarations'(?:[^\x5C']+|\x5C(?:\x5C\x5C)*[\x5C'])*'
matches single quoted string declarations[^"',]+
matches anything else (except commas).The parts of
"(?:[^\x5C"]+|\x5C(?:\x5C\x5C)*[\x5C"])*"
are:[^\x5C"]+
matches anything except the backspace and quote character\x5C(?:\x5C\x5C)*[\x5C"]
matches proper escape sequences like\"
,\\
,\\\"
,\\\\
, etc.不确定您到底在寻找什么,也不确定如何在 SQL 中执行此操作,但是这样还不够:(
以 python 为例)
Not sure exactly what you're seeking, nor yet how to do this in SQL, but isn't something like this sufficient:
(Using python as an example)