Home  >  Article  >  Backend Development  >  Detailed explanation of the steps to implement regular expression group capture in PHP

Detailed explanation of the steps to implement regular expression group capture in PHP

php中世界最好的语言
php中世界最好的语言Original
2018-05-19 10:44:041469browse

这次给大家带来PHP实现正则表达式分组捕获步骤详解,PHP实现正则表达式分组捕获的注意事项有哪些,下面就是实战案例,一起来看一下。

经过测试,发现php正则表达式获取分组捕获是从$0开始,而平时工作中JavaScript中的正则是$1..$9

在提取项目代码中的汉字时,因为当时操作速度很快(赶时间),很担心当时.properties的文件{\d}的数字顺序搞错了:

1、可能从{1}开始,而不是从{0}开始

2、可能跳着写了,比如第一个是{0}第二个需要替换的地方却写着{2}

因为使用人工手动操作的,所以这种情况是难以避免,只能说减少误操作。写完了,得再检查一遍,这个遇到困难了,二三千行的代码,用眼睛一行一行查,那的比较累了,而且还不一定能检查出来。一多就容易出错,而且行与行之间靠的太近了,字又太小…

突然想起来,觉得php可以节省一点时间,读取文件,然后将关键的地方标红…

然后就开始了:php读取文件,然后逐行的读取,使用正则表达式匹配符合{\d}的行,然后将{\d}的地方使用红色进行重点的标记,之后人工去查看每一行是能是符合规则。代码不对,却很受用,至少省了用眼睛去一个一个检查的时间:

这样一眼扫过去,就能很清楚的看出每一行顺序是否都写对了,写错了的行,前面有行号,找到相应行再改改.





检测中文替换文字的正确性

$0", $line);     echo "行".$lineNum.":".$line."


";    }   }  } else {   echo "文件读取失败";  }  fclose($file_handle); ?>

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

php替换文章图片路径到本地服务器步骤详解

PHP对源代码加密方法总结

The above is the detailed content of Detailed explanation of the steps to implement regular expression group capture in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn