简体   繁体   English

修改单个RegEx以匹配字符串中的中间项

[英]Modify a single RegEx to also match a middle term from a string

This RegEx : {=tokenstring[^{}]*(?:{[^{}]*}[^{}]*)*}/g matches this string properly: RegEx{=tokenstring[^{}]*(?:{[^{}]*}[^{}]*)*}/g正确匹配此字符串

{=tokenstring?param1=11&param2={token-identifier}&param3={token-child-identifier}&param4=20}

(That string is a token which is being used in my website. It's value changes dynamically based on the provided request data in the content pages.) (该字符串是我的网站中使用的标记。它的值根据内容页面中提供的请求数据动态更改。)

I would like to add one more condition to the RegEx . 我想再向RegEx添加一个条件。 For example " param3 ", to find if it exists or not in the string . 例如“ param3 ”,以查找字符串中是否存在。

I know this new RegEx : /param3=([^&])/ will get the word " param3 " from the string but how to fit that new RegEx into the original RegEx ? 我知道这个新的RegEx/param3=([^&])/将从字符串中获取单词“ param3 ”但是如何将新的RegEx放入原始的RegEx中

If you don't have a fixed number of parameters, it means you can't write a single regex, since you can't create regular expression with dynamic number of groups. 如果您没有固定数量的参数,则意味着您无法编写单个正则表达式,因为您无法使用动态数量的组创建正则表达式。 However, you can iterate over matches. 但是,您可以迭代匹配。 For example in PHP it would look like this 例如在PHP中,它看起来像这样

#Check if token is right
if (preg_match('/\A\{=tokenstring\?param.+\}\Z/s', $subject)) {
    # Successful match. Now iterate over tokens and their params
    preg_match_all('/(param\d+)=(.+?)(?:&|(?:\}$))/s', $subject, $result, PREG_SET_ORDER);
    for ($matchi = 0; $matchi < count($result); $matchi++) {
        $param = $result[$matchi][0];
        $param_value = $result[$matchi][1];
    }
} else {
    # Match attempt failed
}

UPDATE: The best I could do with a single regex is the case when one parameter could be missing (that is, there're 3 or 4 parameters) 更新:我可以用单个正则表达式做的最好的情况是当一个参数可能丢失时(即,有3个或4个参数)

{=tokenstring\?(?:(param\d+)=(.+?)(?:&|(?:}$)))(?:(param\d+)=(.+?)(?:&|(?:}$)))(?:(param\d+)=(.+?)(?:&|(?:}$)))|(?:(param\d+)=(.+?)(?:&|(?:}$)))

You'll get matched parameters into match groups so you could detect which parameters are missing. 您将匹配参数匹配到匹配组中,以便检测哪些参数丢失。 Also the order of parameters doesn't matter. 参数的顺序也无关紧要。

There's also an if-then-else (?(?=condition)then|else) construct in regex, but if you can have from 1 to 4 parameters, it means you'll have to write a nested regex of three levels, the top one checking for existence of 4 parameters, another one inside the existence of 3 parameters etc, each repeating itself with an insane number of capturing groups. 在regex中还有一个if-then-else (?(?=condition)then|else)构造,但是如果你可以有1到4个参数,那就意味着你必须编写一个三层的嵌套正则表达式,顶部一个检查是否存在4个参数,另一个存在3个参数等,每个参数都以疯狂数量的捕获组重复。

A tentative experiment with an online debugger. 一个在线调试器的试验性实验。 Before: 之前:

{=tokenstring[^{}]*(?:{[^{}]*}[^{}]*)*}

正则表达式可视化

Debuggex Demo "before" Debuggex演示“之前”

After: 后:

{=tokenstring[^{}]*(?:{[^{}]*}[^{}]*)*param3=(?:{[^{}]*}[^{}]*)*}

正则表达式可视化

Debuggex Demo "after" Debuggex演示“之后”

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

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