正则表达式帮助 - Wordpress(搜索正则表达式)

发布于 2024-07-30 20:13:10 字数 1397 浏览 2 评论 0原文

我第一次尝试使用 RE 让我陷入困境。 我通过 Search-Regex 插件 在 WordPress 网站上使用 Regex,并且需要匹配埋在一堆 html 代码中的特定“。 HTML 示例:

provide brand-strengthening efforts for the 10-school conference.&#0160; </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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

哭了丶谁疼 2024-08-06 20:13:10

我假设您想要提取 6a00d834 之后直到第一个 " 的所有内容。因此请尝试以下操作:

/6a00d834([^"]*)"/

第一个分组的匹配将是您要查找的字符串。

I assume that you want to extract everything after 6a00d834 up to the first following ". So try this:

/6a00d834([^"]*)"/

The match of first grouping will than be the string you are looking for.

橘虞初梦 2024-08-06 20:13:10

您的问题并不完全清楚,但也许您的意思是:(

/6a00d834[^"]*"/

即:匹配 6a00d834 后跟零个或多个不是“后跟”的字符)

或者,如果它在您使用的正则表达式引擎中可用,您可以使用非贪婪说明符来限制“*”元字符。 请记住,有关正则表达式的任何问题都取决于您所使用的引擎。 例如:

$ cat input
6a00384foo" more"
$ perl -ne '/(6a00384[^"]*")/; print "$1\n"' input
6a00384foo"
$ perl -ne '/(6a00384.*?")/; print "$1\n"' input
6a00384foo"
$ sed 's/\(6a00384[^"]*"\).*/\1/' input
6a00384foo"
$ sed 's/\(6a00384.*?"\).*/\1/' input
6a00384foo" more"

请注意“?” 在 sed 中不用作非贪婪说明符。

You question is not entirely clear, but perhaps you mean:

/6a00d834[^"]*"/

(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:

$ cat input
6a00384foo" more"
$ perl -ne '/(6a00384[^"]*")/; print "$1\n"' input
6a00384foo"
$ perl -ne '/(6a00384.*?")/; print "$1\n"' input
6a00384foo"
$ sed 's/\(6a00384[^"]*"\).*/\1/' input
6a00384foo"
$ sed 's/\(6a00384.*?"\).*/\1/' input
6a00384foo" more"

Notice that the '?' does not serve as a non-greedy specifier in sed.

东走西顾 2024-08-06 20:13:10

这不行吗?

/(6a00d834.*?)"/

编辑:您在评论中说过您想将 " 替换为 .jpg"; 在这种情况下,这个正则表达式可能会起作用:

/6a00d834.*?(")/

但是,最好的办法可能是使用我提供的第一个正则表达式,并使用如下所示的替换字符串:

'\\1.jpg"'

当然,\\1有替换为您特定的正则表达式引擎用于反向引用的任何内容。

Wouldn't this work?

/(6a00d834.*?)"/

Edit: You said in one of your comments you wanted to replace the " with .jpg"; in that case this regexp would probably work:

/6a00d834.*?(")/

However, the best thing to do is probably to use the first regexp I provided, and use a replacement string that looks like this:

'\\1.jpg"'

Of course, \\1 has to be replaced with whatever you particular regexp engine uses for backreferences.

梦与时光遇 2024-08-06 20:13:10

字符串替换可以与匹配一起完成。 由于您使用的是 PHP,因此请使用 preg_replace

$newstring = preg_replace("/(6a00d834.*?)(\")/", "\\1.jpg\\2", $oldstring)

这会将匹配分为两组,然后插入 '.他们之间的.jpg'。

对于 wordpress 正则表达式插件,使用 /(6a00d834.*?)( ")/ 作为匹配字符串,然后使用 \1.jpg\2 作为替换字符串。

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)

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.

独行侠 2024-08-06 20:13:10

也许使用组运算符?

/6a00d834.*?(")/

然后,根据您的正则表达式 API,您可以提取括号中匹配的内容。

编辑

啊,你想做字符串替换。 我猜你正在使用 Perl。 尝试这个:

s/(6a00d834.*?)(")/\1.jpg\2/

Perhaps use a group operator?

/6a00d834.*?(")/

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:

s/(6a00d834.*?)(")/\1.jpg\2/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文