简体   繁体   English

用正则表达式匹配网址参数

[英]match url params with regex

Im trying to extract url params with regex. 我试图用正则表达式提取URL参数。

here is an example string: param1=val1&param2=val2&adv=val3&param3=val4&param4=val5 这是一个示例字符串: param1=val1&param2=val2&adv=val3&param3=val4&param4=val5

This is the regex im using right now: 这是现在使用的正则表达式即时消息:

(\&)([^=]+)\=([^&]+)

I can't figure out how to match the first param. 我不知道如何匹配第一个参数。 I what to have param1 be in group 2 and val2 in group 3 like the rest of the param matches. 与其余的param匹配一样,我在第2组中具有param1,在第3组中具有val2。

https://regex101.com/r/Qzxyyo/1 https://regex101.com/r/Qzxyyo/1

How can I do this? 我怎样才能做到这一点?

edit: So this seems to work (meaning param1 is in group 2 and val1 in is group3). 编辑:所以这似乎工作(意味着param1在组2中,而val1在组3中)。 But I dont understand why it works or if it is reliable: 但是我不明白为什么它起作用或是否可靠:

(\&|^)([^=]+)\=([^&]+)
(\&)([^=]+)\=([^&]+)

Break it down! 分解吧!

  1. \\& look for a & character \\&寻找&字符
  2. [^=]+ match characters up until it hits a = [^=]+匹配字符直到达到=
  3. \\= match the = character \\=匹配=字符
  4. [^&]+ match characters up until & [^&]+匹配字符直到&
  5. () These define the groups! ()这些定义了组!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM