regex - 反转正则表达式,从正则表达式创建字符串

标签 regex

我在一个多语言网站上工作,并且也选择使用每种语言的自定义 URL,例如:

/en/cities/paris/
/nl/steden/paris/

两者都指向 Cities Controller 的 Index 方法。

在每个页面上都有一个切换语言的选项,它会在我的路由中查找以匹配 Controller 、 View 和语言。

因此,如果我在荷兰语页面上,它会为英文版找到正确的 url,即“cities”而不是“steden”。

一切正常,直到我开始使用更复杂的正则表达式。

我有这些正则表达式可以匹配我想要的 URL:

#^en/cities/([^/]+?)/$#
#^nl/steden/([^/]+?)/$#

在我的代码中,我可以访问正在匹配的变量,在此示例中为“paris”。是否可以“反转”这个正则表达式并让它打印“en/cities/paris/”

如果不是..考虑到 URL 不同,我将如何链接到同一页面的不同版本..最好让它尽可能可编程。

在一个有点类似的问题中,有人回答 (http://stackoverflow.com/a/7070734/616398) 正则表达式的本质是匹配无限数量的结果..所以这可能是不可能的。

从字符串/URL 到一组匹配条件以在 MVC 中使用非常容易,但反过来......不幸的是,没那么多。

最佳答案

是的,这是可能的!对于这种情况,我编写了以下解决方案:

$regex = '#^en/cities/([^/]+?)/$#';
$replace = array('paris');

$result = preg_replace_callback('#^\^|\([^)]*\)|\$$#', function($m)use($replace){
    static $index = 0;
    if($m[0] === '^' || $m[0] === '$'){return '';}
    if(isset($replace[$index])){
        return $replace[$index++];
    }
    return $m[0];
}, substr($regex, 1, -1));
echo $result; // en/cities/paris/

Online demo

我让它变得“灵活”,这样你就可以给它添加更多的值了!

$regex = '#^en/cities/([^/]+?)/region/([^/]+?)$#'; // <<< changed
$replace = array('paris', 'nord'); // <<< changed

$result = preg_replace_callback('#^\^|\([^)]*\)|\$$#', function($m)use($replace){
    static $index = 0;
    if($m[0] === '^' || $m[0] === '$'){return '';}
    if(isset($replace[$index])){
        return $replace[$index++];
    }
    return $m[0];
}, substr($regex, 1, -1));
echo $result; // en/cities/paris/region/nord

Online demo


解释:

$regex = '#^en/cities/([^/]+?)/region/([^/]+?)$#'; // Regex to "reverse"
$replace = array('paris', 'nord'); // Values to "inject"

/*  Regex explanation:
   #   Start delimiter
       ^\^         Match "^" at the begin (we want to get ride of this)
       |           Or
       \([^)]*\)   Match "(", anything zero or more times until ")" is found, ")"
       |           Or
       \$$         Match "$" at the end (we want to get ride of this)
   #   End delimiter
*/

$result = preg_replace_callback('#^\^|\([^)]*\)|\$$#', function($m)use($replace){
    static $index = 0; // Set index 0, note that this variable is only accessible in this (anonymous) function
    if($m[0] === '^' || $m[0] === '$'){return '';} // Get ride of ^/$ at the begin and the end
    if(isset($replace[$index])){ // Always check if it exists, for example if there were not enough values in $replace, this will prevent an error ...
        return $replace[$index++]; // Return the injected value, at the same time increment $index by 1
    }
    return $m[0]; // In case there isn't enough values, this will return ([^/]+?) in this case, you may want to remove it to not include it in the output
}, substr($regex, 1, -1)); // substr($regex, 1, -1) => Get ride of the delimiters
echo $result; // output o_o

Note: This works only on PHP 5.3+

关于regex - 反转正则表达式,从正则表达式创建字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13321395/

相关文章:

ruby 正则表达式 : Matching a < without a matching >

javascript - 如何用另一个字符串数字替换一个数字字符串

javascript - 使用 SSN 正则表达式、屏蔽输入和依赖的 Jquery 验证

javascript - 如果我使用变量而不是直接使用相应的字符串,为什么这个正则表达式不起作用?

php - 仅使用单连字符定界符将字符串转换为 slug

regex - 一个正则表达式,用于制作带有单词边界的首字母缩略词并删除单词前面的字符

regex - 如何在 R 中引用超过\9 的捕获组?

ruby - 搜索字符串返回多个可变长度 Ruby 正则表达式的所有重叠事件的索引

php - 检查字符串是否为整数(包括负数) PHP

regex - 斯卡拉正则表达式 : get last number of string