正则表达式匹配关键字(如果没有用大括号括起来)

发布于 2024-11-08 02:20:26 字数 931 浏览 0 评论 0原文

在 PHP 变量中,我有一些包含一些关键字的文本。这些关键字目前都是大写的。我希望它们保持大写并用大括号括起来,但仅一次。我正在尝试编写升级代码,但每次运行时,它都会将关键字包装在另一组大括号中。

如果是 {KEYWORD},我需要使用什么正则表达式来单独匹配关键字而不匹配它。

例如,文本变量是:

$string = "BLOGNAME has posted COUNT new item(s),

TABLE

POSTTIME AUTHORNAME

You received this e-mail because you asked to be notified when new updates are posted.
Best regards,
MYNAME
EMAIL";

我的升级代码是:

$keywords = array('BLOGNAME', 'BLOGLINK', 'TITLE', 'POST', 'POSTTIME', 'TABLE', 'TABLELINKS', 'PERMALINK', 'TINYLINK', 'DATE', 'TIME', 'MYNAME', 'EMAIL', 'AUTHORNAME', 'LINK', 'CATS', 'TAGS', 'COUNT', 'ACTION');
foreach ($keywords as $keyword) {
    $regex = '|(^\{){0,1}(\b' . $keyword . '\b)(^\}){0,1}|';
    $replace = '{' . $keyword . '}';
    $string = preg_replace($regex, $replace, $string);
}

我的 REGEX 目前根本无法正常工作,它会删除一些空格,并且在每次运行时都会在大多数(但不是全部)关键字周围放置更多大括号。我做错了什么?有人可以纠正我的正则表达式吗?

In a PHP variable I have some text that contains some keywords. These keywords are currently capitalised. I would like them to remain capitalised and be wrapped in curly brackets but once only. I am trying to write upgrade code but each time it runs it wraps the keywords in another set of curly brackets.

What REGEX do I need to use to match the keyword alone without also matching it if it is {KEYWORD}.

For example, the text variable is:

$string = "BLOGNAME has posted COUNT new item(s),

TABLE

POSTTIME AUTHORNAME

You received this e-mail because you asked to be notified when new updates are posted.
Best regards,
MYNAME
EMAIL";

And my upgrade code is:

$keywords = array('BLOGNAME', 'BLOGLINK', 'TITLE', 'POST', 'POSTTIME', 'TABLE', 'TABLELINKS', 'PERMALINK', 'TINYLINK', 'DATE', 'TIME', 'MYNAME', 'EMAIL', 'AUTHORNAME', 'LINK', 'CATS', 'TAGS', 'COUNT', 'ACTION');
foreach ($keywords as $keyword) {
    $regex = '|(^\{){0,1}(\b' . $keyword . '\b)(^\}){0,1}|';
    $replace = '{' . $keyword . '}';
    $string = preg_replace($regex, $replace, $string);
}

My REGEX is currently not working well at all, it is stripping some spaces and also on each run placing more curly brackets around most (but not all) keywords. What am I doing wrong? Can someone correct my regex?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

來不及說愛妳 2024-11-15 02:20:26

您正在寻找否定断言。它们不是使用字符类中的 ^ 语法编写的,而是使用 (? 和 (?!...)(?!...) 编写的代码>.在你的情况下:

'|(?<!\{)(\b' . $keyword . '\b)(?!\})|';

You are looking for negative assertions. They are not written using the ^ syntax as in character classes but as (?<!...) and (?!...). In your case:

'|(?<!\{)(\b' . $keyword . '\b)(?!\})|';
时光与爱终年不遇 2024-11-15 02:20:26
  • 如果关键字不包含特殊字符它将起作用。
  • (A1) 如果源文本不能包含 {keyword} 或需要在结果文本中的关键字周围保留“{}”符号,则可以从正则表达式中删除行(例如,{keyword} 需要 {{keyword}} 进行格式化)

$text = <<<EOF
BLOGNAME has posted COUNT new item(s),

TABLE

POSTTIME AUTHORNAME

You received this e-mail because you asked to be notified when new updates are posted.
Best regards,
MYNAME
EMAIL
EOF;

$aKeywords = array('BLOGNAME', 'BLOGLINK', 'TITLE', 'POST', 'POSTTIME', 'TABLE', 'TABLELINKS', 'PERMALINK', 'TINYLINK', 'DATE', 'TIME', 'MYNAME', 'EMAIL', 'AUTHORNAME', 'LINK', 'CATS', 'TAGS', 'COUNT', 'ACTION');
$keywords = implode('|', $aKeywords);

$reSrch = '/
            (?<!\{)             # (A1) prev symbol is not {
            \b                  # begin of word
            ('.$keywords.') # list of keywords
            \b                  # end of word
            (?!\{)              # (A1) next symbol is not {
            /xm';               //  m - multiline search & x - ignore spaces in regex

$reRepl = '{\1}';

$result = preg_replace($reSrch, $reRepl, $text);

echo '<pre>';
// echo '$reSrch:'.$reSrch.'<hr>';
echo $result.'<br>';
  • It will work if keyword does not contain special char.
  • (A1) rows can be removed from regex, if source text can not contain {keyword} or necessary to leave '{}' symbols around keywords in result text (was {keyword} need {{keyword}} for formatting as example)

$text = <<<EOF
BLOGNAME has posted COUNT new item(s),

TABLE

POSTTIME AUTHORNAME

You received this e-mail because you asked to be notified when new updates are posted.
Best regards,
MYNAME
EMAIL
EOF;

$aKeywords = array('BLOGNAME', 'BLOGLINK', 'TITLE', 'POST', 'POSTTIME', 'TABLE', 'TABLELINKS', 'PERMALINK', 'TINYLINK', 'DATE', 'TIME', 'MYNAME', 'EMAIL', 'AUTHORNAME', 'LINK', 'CATS', 'TAGS', 'COUNT', 'ACTION');
$keywords = implode('|', $aKeywords);

$reSrch = '/
            (?<!\{)             # (A1) prev symbol is not {
            \b                  # begin of word
            ('.$keywords.') # list of keywords
            \b                  # end of word
            (?!\{)              # (A1) next symbol is not {
            /xm';               //  m - multiline search & x - ignore spaces in regex

$reRepl = '{\1}';

$result = preg_replace($reSrch, $reRepl, $text);

echo '<pre>';
// echo '$reSrch:'.$reSrch.'<hr>';
echo $result.'<br>';
一念一轮回 2024-11-15 02:20:26

为什么是正则表达式?只需使用str_replace

foreach ($keywords as $k) {
  $string = str_replace($k, '{'.$k.'}', $string);
}

Why regex? Just use str_replace:

foreach ($keywords as $k) {
  $string = str_replace($k, '{'.$k.'}', $string);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文