将大括号占位符与可变数量的点分隔内部值相匹配

发布于 2024-12-04 12:38:17 字数 247 浏览 2 评论 0原文

我有这样的字符串: {$foo.bar}{$foo.bar.anything}

其中:foo AND bar AND everything === 字母数字

我想通过 preg_match(正则表达式) 匹配 PHP 中的上述 2 个字符串,除了那些没有任何点的字符串例如:{$foo}

I have strings like : {$foo.bar} and {$foo.bar.anything}

WHERE : foo AND bar AND anything === alphanumeric

i want to match the above 2 strings in PHP via preg_match(regular expression) except those without any dot for example : {$foo}

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

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

发布评论

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

评论(7

陪我终i 2024-12-11 12:38:17
/{\$[\da-z]+(?:\.[\da-z]+)+}/i

匹配

{$foo.bar}
{$foo.Bar.anything}
{$foo.bar.anything1.anything2.anything3}
{$foo.bar.anything.a.b.c}

- 不匹配

{$foo}
{$foo.}
{$foo bar}
{$foo.bar anything}
{$foo.bar......anything..}
{$foo.bar.anything.}
{$foo.bar.anything.a.b.c..}

与采用的 Joe 的 PCRE 案例 insensitive 修饰符 将其缩短一点。

特别感谢 sln 让我保持警惕,直到它完美为止。 :)

/{\$[\da-z]+(?:\.[\da-z]+)+}/i

matches

{$foo.bar}
{$foo.Bar.anything}
{$foo.bar.anything1.anything2.anything3}
{$foo.bar.anything.a.b.c}

does not match

{$foo}
{$foo.}
{$foo bar}
{$foo.bar anything}
{$foo.bar......anything..}
{$foo.bar.anything.}
{$foo.bar.anything.a.b.c..}

Adopted Joe’s PCRE case-insensitive modifier to shorten it a bit.

Special thanks to sln for keeping me on my toes until it’s perfect. :)

兔小萌 2024-12-11 12:38:17
\{\$[A-Za-z0-9]+\.[A-Za-z0-9]+\.?[A-Za-z0-9]*\}
\{\$[A-Za-z0-9]+\.[A-Za-z0-9]+\.?[A-Za-z0-9]*\}
无妨# 2024-12-11 12:38:17

您可能需要 preg_match_all 而不是 preg_match - 它会获取所有匹配项,顾名思义,而不仅仅是第一个匹配项。

至于你想要的正则表达式,类似这样的东西应该可以工作

/\{\$[a-z0-9]+\.([a-z0-9\.]+)+\}/i

You probably want preg_match_all rather than preg_match - it gets all matches, as the name suggests, rather than just the first one.

As for the regex you want, something like this should work

/\{\$[a-z0-9]+\.([a-z0-9\.]+)+\}/i
涫野音 2024-12-11 12:38:17

假设 php regex 与 perl 相同,

^\w+\.[\.\w]+$

这意味着以一个或多个字母数字开头,后跟 .,后跟多个字母数字或 .$ 表示一直到字符串末尾。

如果它不能以 . 结尾,那么

^\w+\.[\.\w]+\w$

如果不允许 .. 它会变得很棘手,因为正则表达式引擎无法处理指定多字符子表达式的重复。但如果你的,我认为它是这样的

^\w+(\.\w+)+$

这意味着以一个或多个字母数字开头,后跟一个或多个重复 . 后跟一个或多个字母数字。 $ 表示一直到字符串末尾。

Assuming php regex is the same as perl

^\w+\.[\.\w]+$

That means starting with one or more alphanumeric, followed by a ., followed by a number of alphanumerics or .. The $ means all the way to the end of the string.

If it cannot end with a . then

^\w+\.[\.\w]+\w$

If .. is not allowed It gets tricker as not ell regex engines handle specifying repetitions of multi char sub expressions. But if your's does I think its something like

^\w+(\.\w+)+$

That means starting with one or more alphanumeric, followed one or more repetions of by a . followed one or more alphanumerics. The $ means all the way to the end of the string.

似最初 2024-12-11 12:38:17
\{\$[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+\}

第一个匹配 {$。然后匹配任何字母数字字符串。然后匹配以 . 开头的任何字母数字字符串。然后匹配}

\{\$[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+\}

First match {$. Then match any alphanumeric string. Then match any alphanumeric strings beginning with .. Then match }.

黯淡〆 2024-12-11 12:38:17
/(\{\$[a-z]+\.([a-z][a-z.])*[a-z]+\})/

因此,您首先匹配 foo 和一个点 {$foo.,然后是可选的任何字符和点 {$foo.bar.,最后是另一个字符串。 {$foo.bar.anything}

/(\{\$[a-z]+\.([a-z][a-z.])*[a-z]+\})/

So you first match foo and a dot {$foo., then optionally any characters and dots {$foo.bar., and finally another string of characters. {$foo.bar.anything}

单挑你×的.吻 2024-12-11 12:38:17

这是我对问题的解决方案,根据您确切想要提取的内容,有一些替代方案。

  1. ,前提是它至少包含一个点
  2. 仅提取整个 {$aaa.bbb[.ccc[.ddd ...]]}内容 bbb} 事物(例如 aaa.bbb
  3. 仅考虑由两个或三个组件组成的标签(忽略 {$aaa}{$aaa.bbb.ccc.ddd})。

代码:

<?php

$subject = '{$foo.bar} {$foo.bar.baz} {$foo} {$another-foo.bar} {$foo.bar.baz.boh}';

print "Matching the whole string\n";
preg_match_all(
   '/{\$[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)+}/',
   $subject, $m);
print var_export($m) ."\n\n";

print "Matching only the content\n";
preg_match_all(
   '/{\$([a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)+)}/',
   $subject, $m);
print var_export($m) ."\n\n";

print "Matching for strings containing only 1 or two dots\n";
preg_match_all(
   '/{\$([a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+){1,2})}/',
   $subject, $m);
print var_export($m) ."\n\n";

This is my solution to the problem, with some alternatives depending on what you exactly want to extract.

  1. Extracts just the whole {$aaa.bbb[.ccc[.ddd ...]]} thing, provided that it contains at least one dot
  2. Extracts the content from the {$aaa.bbb} thing (eg. aaa.bbb)
  3. Consider only tags composed by two or three components (ignore {$aaa} or {$aaa.bbb.ccc.ddd}).

Code:

<?php

$subject = '{$foo.bar} {$foo.bar.baz} {$foo} {$another-foo.bar} {$foo.bar.baz.boh}';

print "Matching the whole string\n";
preg_match_all(
   '/{\$[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)+}/',
   $subject, $m);
print var_export($m) ."\n\n";

print "Matching only the content\n";
preg_match_all(
   '/{\$([a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)+)}/',
   $subject, $m);
print var_export($m) ."\n\n";

print "Matching for strings containing only 1 or two dots\n";
preg_match_all(
   '/{\$([a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+){1,2})}/',
   $subject, $m);
print var_export($m) ."\n\n";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文