我正在尝试编写脚本并解析一个文件,
请帮助在php中使用regex来查找和替换以下模式:
来自:"This is a foo/www/bar.txt is a foo/etc/bar.txt“
致:"This is a bar_txt_content is a bar2_txt_content“
一些类似的东西:
$subject = "This is a foo[/www/bar.txt] within a foo[/etc/bar.txt]";
$pattern = '/regex-needed/';
preg_match($pattern, $subject, $matches);
foreach($matches as $match) {
$subject = str_replace('foo['.$match[0].']', file_get_contents($match[0]), $subject);
}我的第二个要求是:
来自:'This is a foo2bar bar ]‘
致:“这是一个返回的”
一些类似的东西:
$subject = 'This is a foo2[bar bar \] bar bar].';
$pattern = '/regex-needed/';
preg_match($pattern, $subject, $matches);
foreach($matches as $match) {
$subject = str_replace('foo2['.$match[0].']', my_function($match[0]), $subject);
}请帮助构建这些模式...
发布于 2012-08-18 21:44:35
找到转义所需的正确正则表达式:
'/foo\[[^\[]*[^\\\]\]/'https://stackoverflow.com/questions/12018611
复制相似问题