正则表达式帮助 - Wordpress (search-regex) [英] Help with Regex - Wordpress (search-regex)
问题描述
我第一次尝试使用 RE 就卡住了.我通过 Search-Regex Plugin 在 Wordpress 网站上使用 Regex 并且需要匹配在一个特定的埋在一堆 html 代码中.HTML 示例:
My first attempt using RE has me stuck. I'm using Regex on a Wordpress website via the Search-Regex Plugin and need to match on a specific " buried within a bunch of html code. HTML example:
provide brand-strengthening efforts for the 10-school conference.  </p>
<p>
<a href="http://www.learfield.com/oldblog/.a/6a00d8345233fa69e201157155a6fc970c-pi">
<img alt="MOvalleyConf500"
border="0"
class="at-xid-6a00d8345233fa69e201157155a6fc970c"
src="http://www.learfield.com/oldblog/.a/6a00d8345233fa69e201157155a6fc970c-800wi"
style="border: 1px solid black; margin: 0px; width: 502px; height: 384px;"
title="MOvalleyConf500" />
</a>
</p>
<p>The photo above
在上面的例子中,有三个目标
In the above example, there are three targets
6a00d8345233fa69e201157155a6fc970c-pi"
6a00d8345233fa69e201157155a6fc970c"
6a00d8345233fa69e201157155a6fc970c-800wi"
我使用的正则表达式是 /6a00d834.*?"/
它定位它们,但是我只想匹配结尾的 "
而不是整个字符串.这些是缺少文件扩展名的图像,所以我需要用 .jpg 替换结尾我理解表达式的替换部分,这是我遇到问题的初始匹配.
The Regex I'm using is /6a00d834.*?"/
it locates them, however I only want to match on the ending "
and not the entire string. These are images that are missing their file extension, so I need to replace the ending " with .jpg" I understand the replacement part of the expression, it's the initial matching I'm having trouble with.
我有一堆这样的 (221),所有目标都以 6a00d834
开头,然后是一些以 "
I have a bunch of these (221), all the targets all begin with 6a00d834
then some random alphanumeric ending with a "
感谢任何见解.谢谢.
从 OP 的评论中添加的编辑:实际上它是在 Wordpress 站点上使用插件 (REGEX) 来查询和替换 SQL 中的数据.我可以使用任何 Perl 兼容的正则表达式.(编辑器注意 - 根据插件,这很可能实际上不是使用 Perl,而是 PHP 的 PCRE 实现.)
Edit added from OP's comment: Actually it's on a Wordpress site using a plugin (REGEX) to query and replace data within SQL. I can use any Perl compatible regex. (Note from editor - depending on the plugin, this is most likely not actually using Perl but PHP's implementation of PCRE.)
推荐答案
字符串替换可以与匹配一起完成.由于您使用的是 PHP,请使用 preg_replace
String replacement can be done along with the matching. Since you're using PHP, use preg_replace
$newstring = preg_replace("/(6a00d834.*?)(\")/", "\\1.jpg\\2", $oldstring)
这将匹配分为两组,然后在它们之间插入.jpg".
This breaks the match into two groups, and then inserts '.jpg' between them.
对于 wordpress 正则表达式插件,请使用/(6a00d834.*?)(")/作为匹配字符串,然后使用 \1.jpg\2 作为替换字符串.
For the wordpress regex plugin, use /(6a00d834.*?)(")/ for the match string, and then use \1.jpg\2 for the replacement string.
这篇关于正则表达式帮助 - Wordpress (search-regex)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!