从正则表达式 PHP 创建字符串 [英] Create string from regular expression PHP

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

问题描述

我正在尝试从正则表达式创建一个字符串.我注意到在 Kohana 框架的路由系统中,您可以使用类似于正则表达式的东西来设置路由.然后您可以创建一个与您设置的路线匹配的网址.我正在尝试做类似的事情,但我找不到一种干净的方法来做到这一点.例如,我有以下正则表达式:

I'm trying to create a string from a regular expression. I noticed that in Kohana framework's routing system you can set a route using something similar to a regular expression. Then you can create an url matching a route you've set. I'm trying to do something similar, but I can't find a clean way to do it. I've for example got the following regular expression:

/^(?<application>\w+)\/(?<controller>\w+)\/(?<method>\w+)\/(?<parameters>\w+)\/?$/

现在我想说应用程序"等于a",控制器"等于b",方法"等于c",参数"等于d".然后我应该得到一个字符串,用指定的值替换部分.我找不到一个好的方法来做到这一点.我基本上想到了两种方法:1) 用指定的值替换正则表达式中的相应值,或者 2) 创建一个自定义的正则表达式语法",您可以轻松地使用它来创建字符串并将其转换为正确的"" 需要时的正则表达式.Kohana 使用后者,但两种方式对我来说听起来都很糟糕.你会怎么做?

Now I want to be able to say that "application" equals "a", "controller" equals "b", "method" equals "c" and "parameters" equals "d". Then I should get a string that replaces the parts with the values specified. I can't find a good way to do this though. I've basically thought of two ways: 1) replace the corresponding values in the regular expression with the specified values, or 2) create a custom "regex syntax" that you can easily be used to create string and convert it to a "proper" regular expression when needed. Kohana uses the latter, but both ways sound quite bad to me. How would you do this?

我会试着澄清一下.例如,我使用 preg_match() 将以下字符串传递给上面显示的正则表达式:myApplication/myController/myMethod/myParameters".这将返回一个包含几个项目的数组,包括 4 个项目,其索引为应用程序"、控制器"、方法"和参数",并具有相应的值.现在我必须使用正则表达式的模式创建字符串myApplication/myController/myMethod/myParameters",而我只有包含 4 个项目的数组.如何使用 PHP 执行此操作?

I'll try to clarify it a bit. I for example pass the following string to the regular expression shown above using preg_match(): "myApplication/myController/myMethod/myParameters". This returns an array that has a couple of items, including 4 items with indexes "application", "controller", "method" and "parameters" with the corresponding values. Now I have to create the string "myApplication/myController/myMethod/myParameters" with the regular expression's pattern while I only have that array with the 4 items. How can I do this using PHP?

推荐答案

鉴于 preg_match 支持命名捕获组(您的正则表达式当然,是使用;更多信息).

It should be pretty straightforward given that preg_match has support for named capturing groups (which your regular expression is, of course, using; more here).

PHP 文档中的示例:

<?php

$str = 'foobar: 2008';

preg_match('/(?P<name>\w+): (?P<digit>\d+)/', $str, $matches);

/* This also works in PHP 5.2.2 (PCRE 7.0) and later, however 
 * the above form is recommended for backwards compatibility */
// preg_match('/(?<name>\w+): (?<digit>\d+)/', $str, $matches);

print_r($matches);

?>

上面的例子会输出:

Array
(
    [0] => foobar: 2008
    [name] => foobar
    [1] => foobar
    [digit] => 2008
    [2] => 2008
)

因此,在您的情况下,您可以使用 $matches 数组,例如$matches['application'].

So in your case, you can use your $matches array, e.g. $matches['application'].

好吧,我没有完全理解这个问题.

Okay, I did not fully understand the question.

使用正则表达式生成字符串的一个明显问题是正则表达式可以匹配无限字符串.例如,/cat\s+rat/ 匹配所有:

An obvious problem with using regular expressions to generate strings is that a regular expression can match infinite strings. For example, /cat\s+rat/ matches all of:

cat rat
cat  rat
cat   rat

但在你的例子中,没有什么是未定义的.

But in your example, nothing is undefined.

因此,考虑到狭窄的用例,您倾向于定义作为正则表达式子集的安全"或可单独生成的语言是一个很好的选择.然后,您可以将出现的 /\(\?P?<(\w+)>\)/ 替换为其中捕获组的值.

So your inclination to define a "safe" or singly-generatable language that is a subset of regular expressions is a good one, given a narrow use case. Then you could replace occurrences of /\(\?P?<(\w+)>\)/ with the value of the capturing group in there.

但是由于 PHP 并没有真正让您在一个步骤中在替换期间仅使用捕获组的值来访问变量,因此您可能必须在多个步骤中执行此操作……例如匹配上述组的所有出现,提取命名组,然后最后(在 $matches 循环中作为 $match)从 中进行简单的字符串替换'(?P<' . $match . '>)''(?<' . $match . '>)'$ 的值$match(虽然比那更安全).

But since PHP doesn’t really let you just use the value of the capturing group to access a variable during replacement in one step, you will likely have to do this in multiple steps… e.g. matching all occurrances of the above group, extracting the named groups, and then finally doing (in a loop over $matches as $match) a simple string substitution from '(?P<' . $match . '>)' and '(?<' . $match . '>)' to the value of $$match (though in a safer way than that).

这篇关于从正则表达式 PHP 创建字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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