使用 RegExp (PREG) 提取函数参数

发布于 2024-07-13 13:03:53 字数 354 浏览 6 评论 0原文

考虑以下函数参数(它们已经从函数中提取):

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

翻身的咸鱼 2024-07-20 13:03:53

这看起来有点令人讨厌,但它有效:

/(?:"(?:[^\x5C"]+|\x5C(?:\x5C\x5C)*[\x5C"])*"|'(?:[^\x5C']+|\x5C(?:\x5C\x5C)*[\x5C'])*'|[^"',]+)+/g

我使用 \x5C 而不是普通的反斜杠字符 \,因为太多可能会造成混淆。

该正则表达式由以下部分组成:

  1. "(?:[^\x5C"]+|\x5C(?:\x5C\x5C)*[\x5C"])*" 匹配双引号字符串声明
  2. '(?:[^\x5C']+|\x5C(?:\x5C\x5C)*[\x5C'])*' 匹配单引号字符串声明
  3. [^" ',]+ 匹配

"(?:[^\x5C"]+|\x5C(?:\x5C\x5C)*[\x5C"] 的部分 。 )*" 是:

  1. [^\x5C"]+ 匹配除退格和引号字符之外的任何内容
  2. \x5C(?:\x5C\x5C)*[\x5C"] 匹配正确的转义序列,如 \"\\\\\"\\\\等

This looks a little nasty but it works:

/(?:"(?:[^\x5C"]+|\x5C(?:\x5C\x5C)*[\x5C"])*"|'(?:[^\x5C']+|\x5C(?:\x5C\x5C)*[\x5C'])*'|[^"',]+)+/g

I used \x5C instead of the plain backslash character \ as too much of those can be confusing.

This regular expression consists of the parts:

  1. "(?:[^\x5C"]+|\x5C(?:\x5C\x5C)*[\x5C"])*" matches double quoted string declarations
  2. '(?:[^\x5C']+|\x5C(?:\x5C\x5C)*[\x5C'])*' matches single quoted string declarations
  3. [^"',]+ matches anything else (except commas).

The parts of "(?:[^\x5C"]+|\x5C(?:\x5C\x5C)*[\x5C"])*" are:

  1. [^\x5C"]+ matches anything except the backspace and quote character
  2. \x5C(?:\x5C\x5C)*[\x5C"] matches proper escape sequences like \", \\, \\\", \\\\, etc.
百思不得你姐 2024-07-20 13:03:53

不确定您到底在寻找什么,也不确定如何在 SQL 中执行此操作,但是这样还不够:(

以 python 为例)

import re
x = '''Monkey, "Blue Monkey", "Red, blue and "Green\\"", 'Red, blue and "Green\\'\''''
l = re.split(',\s*',x)
print x
for a in l:
    print a

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)

import re
x = '''Monkey, "Blue Monkey", "Red, blue and "Green\\"", 'Red, blue and "Green\\'\''''
l = re.split(',\s*',x)
print x
for a in l:
    print a
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文