用于解析请求参数的正则表达式

发布于 2024-07-22 05:37:41 字数 246 浏览 3 评论 0原文

我正在寻找一个与 perl 兼容的正则表达式,它可以解析以下形式的字符串:

param1=value1&...¶m2=value2&...

并仅提取 param1 和 param2 的值。 但

  1. param2可能先于param1
  2. 可能没有param1或param2
  3. param1或param2(或两者)可能有空值,即param1=&...

I am looking for a single perl compatible regex that would parse strings of the form:

param1=value1&...¶m2=value2&...

and extract values for param1 and param2 only. But

  1. param2 may precede param1
  2. There may be no param1 or param2
  3. param1 or param2 (or both) may have empty values, i.e. param1=&...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

病女 2024-07-29 05:37:41

我不会费心编写你自己的请求解析器。 只需使用 CGI.pm 即可。

I wouldn't bother writing your own request parser. Just use CGI.pm.

雨的味道风的声音 2024-07-29 05:37:41
/[?&]([^=]+)=([^=&]+)/g

这将匹配任何由 = 分隔的非 = 字符,并将它们放入 $1 和 $2 中。

或者...

my %argsHash = split(/=|&/, $args);

这会给你一个带有参数和值的散列,看起来效果很好,但 CGI.pm 是一个更好的主意。

/[?&]([^=]+)=([^=&]+)/g

This will match any non = character separated by an = and put them into $1 and $2.

or...

my %argsHash = split(/=|&/, $args);

This will give you a hash with parameters and values which appears to work well, but CGI.pm is all around a better idea.

旧梦荧光笔 2024-07-29 05:37:41

/(.*?)=(.*?)&/ 循环并捕获 $1$2 应该可以工作

/(.*?)=(.*?)&/ looping through and capturing $1 and $2 should work

往事风中埋 2024-07-29 05:37:41

如果这是针对 C++,请考虑使用现有的 CGI 库来为您解析查询参数,而不是自己重新发明此类东西。

其中一个库是 cgic,它是一个 CGI 库:http:// www.boutell.com/cgic/#functions

If this is for C++, consider using an existing CGI library that parses the query parameters for you instead of reinventing that sort of thing yourself.

One such library is cgic, a CGI library for C: http://www.boutell.com/cgic/#functions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文