dongque1462 2018-04-19 01:43
浏览 200

正则表达式 - 两个被捕获的组,其中一个或两个必须出现

I have a following link structure:

/type1
/type2
/type3

those links correspond to the default language of the site. Unfortunately the client didn't want to add the default language in front of the URL for consistency, therefore only other languages will have URLs like:

/en    
/en/type1
/de/type2
/de
/fr/type3
/fr

There are a lot of other variables but only this part is dynamic. My Regex starts as following:

(en|de|fr)?\/?(type1|type2|type3)?\/?

which basically means capture the language if exists, and then capture the page if exists. But it performs a lot more matches than required and also would capture empty string etc.

I'm trying to figure out how to capture all these options:

/en
/en/type1
/type1

in one expression, of course if possible. How can I make one of the two groups to be required, so basically the URL has either both or one of them but never none? I looked at backreferences in conjunction with look-aheads but I think I'm missing some crucial information here...

I would like to preserve the groups so that group1 = language and group2 = page

  • 写回答

2条回答 默认 最新

  • dongzhuo8210 2018-04-19 02:06
    关注

    I can't think of a way to do what you want with a single regex. But, another possibility would be to use a single regex to just match URL patterns which you want. Then, use a short PHP script to extract the language (if it exists) and page:

    $path = "/de/type1";
    if (preg_match("/^(?:\/(?:en|de|fr))?(?:\/(?:type1|type2|type3))?$/i", $path, $match)) {
        $parts = preg_split("/\//", $path);
        if (sizeof($parts) == 3) {
            echo "language: " . $parts[1] . ", page: " . $parts[2];
        }
        else {
            if (preg_match("/^(?:en|de|fr)$/i", $parts[1], $match)) {
                echo "language: " . $parts[1] . ", page:";
            }
            else {
                echo "language: default, page: " . $parts[1];
            }
        }
    }
    

    Demo

    This is the pattern I used for matching:

    ^(?:/(?:en|de|fr))?(?:/(?:type1|type2|type3))?$
    

    It allows for /(type1|type2|type3), optionally preceded by a language path.

    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像