使用 GREP / RegEx 查找和替换字符串
因此,我正在尝试将数据库从 Textpattern CMS 迁移到更通用的数据库。文章中有一些特定于文本模式的命令来提取图像。我想将它们转换为通用 HTML 图像链接。目前,它们在 sql 文件中看起来像这样:
<txp:upm_image image_id="4" form="dose" />
我想将它们变成更像这样的东西:
<img src="4.jpg" class="dose" />
我很幸运地使用 TextWrangler 做了一些正则表达式的东西,但我很困惑。关于如何查找和查找的任何想法替换所有这些图像路径?
编辑: 为了供将来参考,以下是我最终在 PHP 中执行的输出操作:
$body = $post['Body_html'];
$pattern = '/txp:upm_image image_id="([0-9]+)" form="([^"]*)"/i';
$replacement = 'img src="/images/$1.jpg" class="$2"';
$body = preg_replace($pattern, $replacement, $body);
// outputed <img src="/images/59.jpg" class="dose" />
So, I'm trying to migrate a database from Textpattern CMS to something more generic. There are some textpattern-specific commands inside of articles that pull in images. I want to turn these into generic HTML image links. At the moment, they look like this in the sql file:
<txp:upm_image image_id="4" form="dose" />
I want to turn these into something more like this:
<img src="4.jpg" class="dose" />
I've had some luck with TextWrangler doing some regex stuff, but I'm stumped. Any ideas on how to find & replace all of these image paths?
EDIT:
For future reference, here's what I ended up doing in PHP to output it:
$body = $post['Body_html'];
$pattern = '/txp:upm_image image_id="([0-9]+)" form="([^"]*)"/i';
$replacement = 'img src="/images/$1.jpg" class="$2"';
$body = preg_replace($pattern, $replacement, $body);
// outputed <img src="/images/59.jpg" class="dose" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不会使用 grep; 如果你的类有字母数字字符,那就是你想要的 sed
,使用 [[:alnum:]]
(适用于 macos darwin)
I wouldn't use grep; it's sed you want
if your class has alphanumeric characters, use [[:alnum:]]
(works on macos darwin)
不确定您使用的是哪个工具,但尝试此正则表达式解决方案:搜索此:
并替换为:
请注意,这仅适用于与您的示例具有相同形式的 txp 标签。如果存在具有额外属性的 txp 标签,或者它们的顺序不同,则会失败。
Not sure which tool you are using but try this regex solution: Search for this:
And replace with this:
Note that this only works for txp tags having the same form as your example. It will fail if there are txp tags having extra attributes, or if they are in a different order.