使用regex验证URL查询字符串

Hel*_*röm 6 regex validation expression

我正在尝试使用正则表达式验证查询字符串.请注意,我不是要匹配值,而是验证其语法.我这样做是为了练习正则表达式,所以我很感激帮助而不是"使用这个lib",虽然看看它如何在lib中完成它会对我有所帮助,所以如果你有一个,请告诉我.

所以,这将是先决条件:

  • 它必须以问号开头.
  • 它可能包含带有或不带有由等号分隔的值的键,由&符号分隔的对.

我已经相当远了,但是我在正则表达式中匹配时遇到的问题是等号和符号必须按特定顺序排列而不必重复匹配组.这是我到目前为止所得到的:

#^\?([\w\-]+((&|=)([\w\-]+)*)*)?$#
Run Code Online (Sandbox Code Playgroud)

它正确匹配?abc=123&def=345,但它也错误匹配例如?abc=123=456.

我可能会过度杀戮并做类似......

/^\?([\w\-]+=?([\w\-]+)?(&[\w\-]+(=?[\w\-]*)?)*)?$/
Run Code Online (Sandbox Code Playgroud)

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

我如何告诉正则表达式值之间的分隔符必须在重复匹配组之间迭代,&还是=不重复匹配组或灾难性的反向跟踪?

谢谢.

编辑:

我想澄清一点,这不适用于现实世界的实施; 为此,应该使用您的语言中最有可能的内置库.问这个问题是因为我想提高我的正则表达式技能,解析查询字符串似乎是一个有意义的挑战.

Boh*_*ian 9

这似乎是你想要的:

^\?([\w-]+(=[\w-]*)?(&[\w-]+(=[\w-]*)?)*)?$
Run Code Online (Sandbox Code Playgroud)

查看现场演示

这将每个"对"视为一个键,后跟一个可选值(可能是空白),并且具有第一对,后面是可选的&另一对,整个表达式(除了前导?)是可选的.这样做可以防止匹配?&abc=def

还要注意,连字符在字符类中的最后一个时不需要转义,允许略微简化.

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

^\?(\w+(=[\w-]*)?(&\w+(=[\w-]*)?)*)?$
Run Code Online (Sandbox Code Playgroud)


Ami*_*oki 5

您可以使用这个正则表达式:

^\?([^=]+=[^=]+&)+[^=]+(=[^=]+)?$
Run Code Online (Sandbox Code Playgroud)

它的作用是:

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  \?                       '?'
--------------------------------------------------------------------------------
  (                        group and capture to \1 (1 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    [^=]+                    any character except: '=' (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    =                        '='
--------------------------------------------------------------------------------
    [^=]+                    any character except: '=' (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    &                        '&'
--------------------------------------------------------------------------------
  )+                       end of \1 (NOTE: because you are using a
                           quantifier on this capture, only the LAST
                           repetition of the captured pattern will be
                           stored in \1)
--------------------------------------------------------------------------------
  [^=]+                    any character except: '=' (1 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  (                        group and capture to \2 (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    =                        '='
--------------------------------------------------------------------------------
    [^=]+                    any character except: '=' (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )?                       end of \2 (NOTE: because you are using a
                           quantifier on this capture, only the LAST
                           repetition of the captured pattern will be
                           stored in \2)
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
Run Code Online (Sandbox Code Playgroud)

  • https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CCkQFjAA&url=http%3A%2F%2Frick.measham.id.au%2Fpaste% 2Fexplain.pl%3Fregex%3D%255Cd%252B(.%255Cd%252B)%253F(%255BeE%255D%255Cd%252B)%253F&ei=A7aIU4eLIdiiugSp84KgDg&usg=AFQjCNHAWE1AKnZQeZkSODfrlt9lKjm28 g&sig2=q5lxzXXvb5y_-DazXkiFkw&bvm=bv.67720277,d.c2E (2认同)