使用正则表达式验证URL查询字符串 [英] Validate URL query string with regex

查看:190
本文介绍了使用正则表达式验证URL查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用正则表达式验证查询字符串。请注意,我不会尝试匹配值,但验证其语法。我正在这样做来实现正则表达式,所以我会感谢帮助,而不是使用这个lib,虽然看到它可能在一个lib可以帮助我,所以告诉我,如果你有一个。 p>

所以,这将是先决条件:




  • 它必须以一个问号开头。

  • 它可能包含带有或不带有等号的键分隔的键,由&号分隔的对。


$ b $我已经很远了,但是我无法在正则表达式匹配,等号和符号必须是一定的顺序,而不必重复匹配组。这是我到目前为止已经有的:

 #^ \?([\w\  - ] + (& | =)([\w\  - ] +)*)*)?$#

它正确匹配?abc = 123& def = 345 ,但它也不正确地匹配,例如?abc = 123 = 456



我可以过度杀戮,做一些像...

 code> / ^ \([\w\  - ] + =([\w\  - ] +)(&安培;????[\w\  - ] +(= [ \w\  - ] *)?)*)?$ / 

...但我不要重复相同的匹配组。



我如何告诉正则表达式值之间的分隔符必须在和amp之间迭代; = 而不重复匹配组或灾难性的追踪?



谢谢。



编辑:



我想澄清一点,为实际执行;为此,应该使用最有可能的语言的内置图书馆。这个问题被问到是因为我想提高我的正则表达式技巧,解析一个查询字符串似乎是一个有益的挑战。

解决方案

似乎是你想要的:

  ^ \?([\w  - ] +(= [\w- ] *)?(& [\w  - ] +(= [\w  - ] *)?)*)?$ 

请参阅现场演示



此将每个对视为一个键,然后是可选值(可能为空),并具有第一对,后跟可选的& 然后另一对,整个表达式(除了领先的)是可选的。这样做可以避免匹配?& abc = def



另请注意,连字符不需要转义最后在字符类中,允许轻微的简化。



您似乎希望在键或值中的任何位置允许连字符。如果键需要连字符:

  ^ \?(\w +(= [\w  - ] *) ?(& \w +(= [\w  - ] *)?)*)?$ 


I'm trying to validate a query string with regex. Note that I'm not trying to match out the values, but validate its syntax. I'm doing this to practice regex, so I'd appreciate help rather than "use this lib", although seing how it may have been done in a lib would help me, so show me if you've got one.

So, this would be the prerequisites:

  • It must start with a questionmark.
  • It may contain keys with or without values separated by an equals-sign, pairs separated by ampersand.

I've got pretty far, but I'm having trouble matching in regex that the equals-sign and ampersand must be in a certain order without having to repeat match groups. This is what I've got so far:

#^\?([\w\-]+((&|=)([\w\-]+)*)*)?$#

It correctly matches ?abc=123&def=345, but it also incorrectly matches for example ?abc=123=456.

I could go overkill and do something like...

/^\?([\w\-]+=?([\w\-]+)?(&[\w\-]+(=?[\w\-]*)?)*)?$/

... but I don't want to repeat the match groups which are the same anyway.

How can I tell regex that the separators between values must iterate between & and = without repeating match groups or catastrophic back tracking?

Thank you.

Edit:

I'd like to clarify that this is not meant for a real-world implementation; for that, the built-in library in your language, which is most likely available should be used. This question is asked because I want to improve my regex skills, and parsing a query string seemed like a rewarding challenge.

解决方案

This seems to be what you want:

^\?([\w-]+(=[\w-]*)?(&[\w-]+(=[\w-]*)?)*)?$

See live demo

This considers each "pair" as a key followed by an optional value (which maybe blank), and has a first pair, followed by an optional & then another pair,and the whole expression (except for the leading?) is optional. Doing it this way prevents matching ?&abc=def

Also note that hyphen doesn't need escaping when last in the character class, allowing a slight simplification.

You seem to want to allow hyphens anywhere in keys or values. If keys need to be hyphen free:

^\?(\w+(=[\w-]*)?(&\w+(=[\w-]*)?)*)?$

这篇关于使用正则表达式验证URL查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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