从正则表达式PHP创建字符串(Create string from regular expression PHP)

编程入门 行业动态 更新时间:2024-10-26 21:24:45
从正则表达式PHP创建字符串(Create string from regular expression PHP)

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

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

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

编辑:我会尝试澄清一下。 我举例说明使用preg_match()将以下字符串传递给上面显示的正则表达式:“myApplication / myController / myMethod / myParameters”。 这将返回一个包含几个项目的数组,包括4个带有索引“application”,“controller”,“method”和“parameters”的项目以及相应的值。 现在我必须使用正则表达式的模式创建字符串“myApplication / myController / myMethod / myParameters”,而我只有4个项目的数组。 我怎么能用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. 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+)\/?$/

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?

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". 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支持命名捕获组 (当然,你的正则表达式使用; 更多这里 )。

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'] 。


编辑:好的,我没有完全理解这个问题。

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

cat rat cat rat cat rat

等等

但在你的例子中,没有任何未定义。

因此,考虑到一个狭窄的用例,您倾向于定义一个“安全”或单一可生成语言,这是正则表达式的一个子集。 然后你可以将/\(\?P?<(\w+)>\)/替换为其中捕获组的值。

但是由于PHP实际上并没有让你只使用捕获组的值来在替换过程中一步访问变量,所以你可能需要在多个步骤中执行此操作...例如,匹配上述组的所有出现,提取命名组,然后最终做(在$match的循环$match )一个简单的字符串替换来自'(?P<' . $match . '>)'和'(?<' . $match . '>)'到$$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).

An example from PHP documentation:

<?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:

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

So in your case, you can use your $matches array, e.g. $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 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.

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).

更多推荐

本文发布于:2023-08-01 03:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1352789.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   正则表达式   PHP   Create   regular

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!