简体   繁体   English

从正则表达式 PHP 创建字符串

[英]Create string from regular expression PHP

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.我注意到在 Kohana 框架的路由系统中,您可以使用类似于正则表达式的方式设置路由。 Then you can create an url matching a route you've set.然后,您可以创建与您设置的路线匹配的 url。 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+)\/?$/

Now I want to be able to say that "application" equals "a", "controller" equals "b", "method" equals "c" and "parameters" equals "d".现在我想说“应用程序”等于“a”,“控制器”等于“b”,“方法”等于“c”,“参数”等于“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.我基本上想到了两种方法:1)用指定的值替换正则表达式中的相应值,或者 2)创建一个自定义的“正则表达式语法”,您可以轻松地使用它来创建字符串并将其转换为“适当的" 需要时的正则表达式。 Kohana uses the latter, but both ways sound quite bad to me. Kohana 使用后者,但两种方式对我来说听起来都很糟糕。 How would you do this?你会怎么做?

Edit: 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".例如,我使用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.这将返回一个包含几个项目的数组,其中包括 4 个项目,其索引为“应用程序”、“控制器”、“方法”和“参数”以及相应的值。 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.现在我必须使用正则表达式的模式创建字符串“myApplication/myController/myMethod/myParameters”,而我只有包含 4 个项目的数组。 How can I do this using PHP?如何使用 PHP 做到这一点?

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 ).鉴于preg_match支持命名捕获组(当然,您的正则表达式正在使用它;更多信息在这里),这应该非常简单。

An example from PHP documentation : 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);

?>

The above example will output:上面的例子将 output:

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

So in your case, you can use your $matches array, eg $matches['application'] .因此,在您的情况下,您可以使用$matches数组,例如$matches['application']


Edit: Okay, I did not fully understand the question.编辑:好的,我没有完全理解这个问题。

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\s+rat/匹配所有:

cat rat
cat  rat
cat   rat

etc.等等

But in your example, nothing is undefined.但在你的例子中,没有什么是未定义的。

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.然后你可以用那里的捕获组的值替换/\(\?P?<(\w+)>\)/的出现。

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… eg 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 并没有真正让您在替换期间只使用捕获组的值来访问变量,因此您可能必须分多个步骤执行此操作……例如匹配上述组的所有出现,提取命名的组,然后最后(在$matches作为$match的循环中)从'(?P<'. $match. '>)''(?<'. $match. '>)'进行简单的字符串替换到$$match的值(尽管以比这更安全的方式)。

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

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