简体   繁体   English

在正则表达式中循环

[英]Looping within a regular expression

can regex able to find a patter to this? 正则表达式可以找到一个模式吗?

{{foo.bar1.bar2.bar3}} {{foo.bar1.bar2.bar3}}

where in the groups would be 小组中的什么地方

$1 = foo $2 = bar1 $3 = bar2 $4 = bar3 and so on.. $ 1 = foo $ 2 = bar1 $ 3 = bar2 $ 4 = bar3,依此类推。

it would be like re-doing the expression over and over again until it fails to get a match. 就像一遍又一遍地重新执行表达式,直到无法匹配为止。

the current expression i am working on is 我正在处理的当前表达式是

(?:\{{2})([\w]+).([\w]+)(?:\}{2})

Here's a link from regexr. 这是regexr的链接。

http://regexr.com?3203h http://regexr.com?3203h

-- -

ok I guess i didn't explain well what I'm trying to achieve here. 好的,我想我没有很好地解释我要在这里实现的目标。

let's say I am trying to replace all 假设我正在尝试取代所有

.barX inside a {{foo . .barX放在{{foo . . }} }}

my expected results should be 我的预期结果应该是

$foo->bar1->bar2->bar3

This should work, assuming no braces are allowed within the match: 假设比赛中没有大括号,这应该可以工作:

preg_match_all(
    '%(?<=     # Assert that the previous character(s) are either
     \{\{      # {{
    |          # or
     \.        # .
    )          # End of lookbehind
    [^{}.]*    # Match any number of characters besides braces/dots.
    (?=        # Assert that the following regex can be matched here:
     (?:       # Try to match
      \.       #  a dot, followed by
      [^{}]*   #  any number of characters except braces
     )?        # optionally
     \}\}      # Match }}
    )          # End of lookahead%x', 
    $subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];

I don't think so, but it's relatively painless to just split the string on periods like so: 我不这么认为,但是像这样在句点上分割字符串是相对容易的:

$str = "{{foo.bar1.bar2.bar3}}";
$str = str_replace(array("{","}"), "", $str);
$values = explode(".", $str);

print_r($values);  // Yields an array with values foo, bar1, bar2, and bar3

EDIT: In response to your question edit, you could replace all barX in a string by doing the following: 编辑:响应您的问题编辑,您可以通过执行以下操作来替换字符串中的所有barX

$str = "{{foo.bar1.bar2.bar3}}";
$newStr = preg_replace("#bar\d#, "hi", $str);

echo $newStr; // outputs "{{foo.hi.hi.hi}}"

I'm not a PHP person, but I managed to construct this piece of code here: 我不是PHP的人,但是我设法在这里构造了这段代码:

preg_match_all("([a-z0-9]+)",
"{{foo.bar1.bar2.bar3}}",
$out, PREG_PATTERN_ORDER);

foreach($out[0] as $val)
{
echo($val);
echo("<br>");
}

The code above prints the following: 上面的代码显示以下内容:

foo 
bar1
bar2
bar3

It should allow you to exhaustively search a given string by using a simple regular expression. 它应该允许您使用简单的正则表达式穷举搜索给定的字符串。 I think that you should also be able to get what you want by removing the braces and splitting the string. 我认为您还应该能够通过除去括号和拆分字符串来获得所需的内容。

I don't know the correct syntax in PHP, for pulling out the results, but you could do: 我不知道PHP中正确的语法来提取结果,但是您可以这样做:

\{{2}(\w+)(?:\.(\w+))*\}{2}

That would capture the first hit in the first capturing group and the rest in second capturing group. 这将捕获第一个捕获组中的第一个匹配项,并捕获第二个捕获组中的其他匹配项。 regexr.com is lacking the ability to show that as far as I can see though. 据我所知,regexr.com缺乏能力证明这一点。 Try out Expresso , and you'll see what I mean. 试用Expresso ,您会明白我的意思。

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

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