简体   繁体   English

PHP-正则表达式删除引号并添加大括号?

[英]PHP - Regex to remove quotes and add braces?

Well, I hate to admit it but I have a hard time with REGEX, I could never find a decent tutorial on how expressions should be set up. 好吧,我不愿意承认这一点,但是我对REGEX感到很难,我再也找不到关于如何设置表达式的不错的教程。

So say I have something like this 所以说我有这样的事情

context['something']

and I want to change all occurrences to 我想将所有事件更改为

context[something] 

Then I have 那我有

' . $var . ' 

and I want to change all occurrences to 我想将所有事件更改为

{var} 

This is the current concept but I am having trouble with the regex part. 这是当前的概念,但是我在正则表达式部分遇到了麻烦。 I am using str_replace but with language changes I don't think it would stable enough that way. 我正在使用str_replace,但是随着语言的改变,我认为这样不够稳定。

Here is my attempt. 这是我的尝试。

$codes = array (
         '/(\' \. \$)(.+)( \. \')/',
        '/(\[\')(.+)(\'\])/'
);
$html = array (
        '{\\2}',
        '[\\2]',
);
$data = preg_replace($codes, $html, $data); 

It works until you get allot of them in a file and then it goes bad. 它起作用,直到您在文件中分配了它们,然后变坏了。

This is the current setup 这是当前设置

// these are temp need a better replace system
$data = str_replace("' . $", "{", $data);
$data = str_replace(" . '", "}", $data);
$data = str_replace("<?php", "", $data);
$data = str_replace("?>", "", $data);
$data = str_replace('context[\'forum_name\']', 'context[forum_name]', $data); 

Just need a proper way to comment these so they can be converted back later on during save. 只需要一种适当的方式来注释这些内容,以便以后在保存期间可以将它们转换回去。

Can anyone please help? 谁能帮忙吗?

Thanks :) 谢谢 :)

Make your matches non-greedy . 使您的比赛不贪心

Change 更改

.+

to

.+?

It works!! 有用!!

Explanation: 说明:

The quantifier + is greedy by default. 默认情况下,量词+为贪婪。

Consider the regex \\[(.+)\\] which tries to match and capture everything between the [ and ] . 考虑正则表达式\\[(.+)\\] ,它试图匹配并捕获[]之间的所有内容。 On the input [foo] it works fine and foo is captured. 在输入[foo]可以正常工作,并且可以捕获foo But on the input [foo] and [bar] it'll capture foo] and [bar !!! 但是在输入[foo] and [bar]它将捕获foo] and [bar !!!! This is the greedy behavior in action which makes + to consume as much as it can. 这是行动中的贪婪行为,使+尽可能多地消耗。 By making the match non-greedy \\[(.+?)\\] we tell + to consume as less as it can but still trying to match. 通过使匹配为非贪婪\\[(.+?)\\]我们告诉+消耗尽可能少的能量,但仍在尝试匹配。 So in this case with the same input it'll capture foo . 因此,在这种情况下,使用相同的输入将捕获foo

Some tips: 一些技巧:

There is no real need of the 1st and the 3rd group in your regex: '/(\\' \\. \\$)(.+)( \\. \\')/' . 正则表达式中没有第一和第三组的实际需求: '/(\\' \\. \\$)(.+)( \\. \\')/' So you can drop the 1st and 3rd pair of (...) . 因此,您可以删除(...)的第一对和第三对。 If you just want to group use (?:..) rather than (..) which is used for grouping + capturing. 如果只想分组使用(?:..)而不是用于分组+捕获的(..)

The use of \\\\n in the replacement part is discouraged. 不建议在替换部分中使用\\\\n It should be used only as back-reference in the regex. 它仅应在正则表达式中用作反向引用。 To use capture groups in the replacement part use $n . 要在替换部件中使用捕获组,请使用$n

Program with above changes 进行上述更改的程序

我发现该工具在学习正则表达式时非常有用。

Here's one: 这是一个:

preg_replace("/context\['([^\']+)'\]/", "context[$1]", "context['bob']");

instead of using a non-greedy match, we just look for anything that's not a quotation mark. 而不是使用非贪婪的匹配,我们只是寻找不是引号的任何东西。

And the other: 和另一个:

preg_replace("/' . [$]([^ ]+) . '/", '{$1}', '\' . $foo . \'');

This one's a bit trickier. 这个有点棘手。 We have to trap the $ in a set so it's not treated as the end-of-string character. 我们必须将$限制在一个集合中,以便不将其视为字符串结尾字符。 And the {$1} has to be in single quotes so PHP doesn't interpret it + the braces as a variable. 并且{$ 1}必须用单引号引起来,因此PHP不会将其+大括号解释为变量。

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

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