正则表达式帮助 - Wordpress(搜索正则表达式)
我第一次尝试使用 RE 让我陷入困境。 我通过 Search-Regex 插件 在 WordPress 网站上使用 Regex,并且需要匹配埋在一堆 html 代码中的特定“。 HTML 示例:
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
在上面的示例中,有三个目标
6a00d8345233fa69e201157155a6fc970c-pi"
6a00d8345233fa69e201157155a6fc970c"
6a00d8345233fa69e201157155a6fc970c-800wi"
我使用的正则表达式是 /6a00d834.*?"/
它找到了它们,但是我只想匹配结尾的 "
而不是整个字符串。这些图像缺少文件扩展名,因此我需要将结尾的 " 替换为 .jpg" 我了解替换部分表达式,这是我遇到问题的初始匹配,
我有一堆这样的(221),所有目标都以 6a00d834
开头,然后是一些以 "< 结尾的随机字母数字。 /code>
感谢任何见解。 谢谢。
从OP评论中添加的编辑:实际上它是在一个Wordpress网站上使用插件(REGEX)来查询和替换SQL中的数据。 我可以使用任何 Perl 兼容的正则表达式。 (编辑注意 - 根据插件的不同,这很可能实际上不是使用 Perl,而是 PHP 的 PCRE 实现。)
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"
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.
I have a bunch of these (221), all the targets all begin with 6a00d834
then some random alphanumeric ending with a "
Appreciate any insight. Thanks.
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.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我假设您想要提取
6a00d834
之后直到第一个"
的所有内容。因此请尝试以下操作:第一个分组的匹配将是您要查找的字符串。
I assume that you want to extract everything after
6a00d834
up to the first following"
. So try this:The match of first grouping will than be the string you are looking for.
您的问题并不完全清楚,但也许您的意思是:(
即:匹配 6a00d834 后跟零个或多个不是“后跟”的字符)
或者,如果它在您使用的正则表达式引擎中可用,您可以使用非贪婪说明符来限制“*”元字符。 请记住,有关正则表达式的任何问题都取决于您所使用的引擎。 例如:
请注意“?” 在 sed 中不用作非贪婪说明符。
You question is not entirely clear, but perhaps you mean:
(That is: match 6a00d834 followed by zero or more characters that are not a " followed by a ")
Alternatively, if it is available in the regex engine you are using, you can use a non-greedy specifier to limit the '*' meta-character. Keep in mind that any question about regex's is dependent on the engine you are using. For example:
Notice that the '?' does not serve as a non-greedy specifier in sed.
这不行吗?
编辑:您在评论中说过您想将
"
替换为.jpg"
; 在这种情况下,这个正则表达式可能会起作用:但是,最好的办法可能是使用我提供的第一个正则表达式,并使用如下所示的替换字符串:
当然,
\\1
有替换为您特定的正则表达式引擎用于反向引用的任何内容。Wouldn't this work?
Edit: You said in one of your comments you wanted to replace the
"
with.jpg"
; in that case this regexp would probably work:However, the best thing to do is probably to use the first regexp I provided, and use a replacement string that looks like this:
Of course,
\\1
has to be replaced with whatever you particular regexp engine uses for backreferences.字符串替换可以与匹配一起完成。 由于您使用的是 PHP,因此请使用 preg_replace
这会将匹配分为两组,然后插入 '.他们之间的.jpg'。
对于 wordpress 正则表达式插件,使用 /(6a00d834.*?)( ")/ 作为匹配字符串,然后使用 \1.jpg\2 作为替换字符串。
String replacement can be done along with the matching. Since you're using PHP, use preg_replace
This breaks the match into two groups, and then inserts '.jpg' between them.
For the wordpress regex plugin, use /(6a00d834.*?)(")/ for the match string, and then use \1.jpg\2 for the replacement string.
也许使用组运算符?
然后,根据您的正则表达式 API,您可以提取括号中匹配的内容。
编辑
啊,你想做字符串替换。 我猜你正在使用 Perl。 尝试这个:
Perhaps use a group operator?
Then, depending on your regex API, you can pull out just what is matched in the parens.
Edit
Ah, you want to do string replacement. I'll guess you're using Perl. Try this: