首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >正则表达式function[filename]模式和function[string_with_escaped_characters]模式

正则表达式function[filename]模式和function[string_with_escaped_characters]模式
EN

Stack Overflow用户
提问于 2012-08-18 20:35:49
回答 4查看 142关注 0票数 0

我正在尝试编写脚本并解析一个文件,

请帮助在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“

一些类似的东西:

代码语言:javascript
运行
复制
$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 ]‘

致:“这是一个返回的”

一些类似的东西:

代码语言:javascript
运行
复制
$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);
}

请帮助构建这些模式...

EN

Stack Overflow用户

回答已采纳

发布于 2012-08-18 21:44:35

找到转义所需的正确正则表达式:

代码语言:javascript
运行
复制
'/foo\[[^\[]*[^\\\]\]/'
票数 0
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12018611

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档