正则表达式:获取括号之外的内容

发布于 2024-09-19 17:53:42 字数 240 浏览 4 评论 0原文

我正在使用 PHP,并且有如下文本:

first [abc] middle [xyz] last

我需要获取括号内和外的内容。在 StackOverflow 中搜索时,我发现了一种获取内部内容的模式:

preg_match_all('/\[.*?\]/', $m, $s)

现在我想知道获取外部内容的模式。

问候!

I'm using PHP and I have text like:

first [abc] middle [xyz] last

I need to get what's inside and outside of the brackets. Searching in StackOverflow I found a pattern to get what's inside:

preg_match_all('/\[.*?\]/', $m, $s)

Now I'd like to know the pattern to get what's outside.

Regards!

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

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

发布评论

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

评论(4

我早已燃尽 2024-09-26 17:53:42

您可以使用 preg_split 来实现此目的:

$input ='first [abc] middle [xyz] last';
$arr = preg_split('/\[.*?\]/',$input);
print_r($arr);

输出:

Array
(
    [0] => first 
    [1] =>  middle 
    [2] =>  last
)

这允许输出中存在一些周围的空格。如果您不需要它们,可以使用:

$arr = preg_split('/\s*\[.*?\]\s*/',$input);

preg_split 根据模式拆分字符串。这里的模式是 [ 后跟任何内容,然后是 ]。匹配任何内容的正则表达式是.*。另外 [] 是用于 char 类的正则表达式元字符。由于我们想要从字面上匹配它们,因此我们需要对它们进行转义以获得 \[.*\].* 默认是贪婪的,会尝试尽可能多的匹配。在这种情况下,它将匹配 abc] 中间的 [xyz。为了避免这种情况,我们通过附加 ? 来使其不贪婪,以给出 \[.*?\]。由于我们对这里任何内容的定义实际上意味着 ] 之外的任何内容,我们也可以使用 \[[^]]*?\]

编辑:

如果如果您想要提取 [] 内部和外部的单词,可以使用:

$arr = preg_split('/\[|\]/',$input);

将字符串拆分为 []

You can use preg_split for this as:

$input ='first [abc] middle [xyz] last';
$arr = preg_split('/\[.*?\]/',$input);
print_r($arr);

Output:

Array
(
    [0] => first 
    [1] =>  middle 
    [2] =>  last
)

This allows some surrounding spaces in the output. If you don't want them you can use:

$arr = preg_split('/\s*\[.*?\]\s*/',$input);

preg_split splits the string based on a pattern. The pattern here is [ followed by anything followed by ]. The regex to match anything is .*. Also [ and ] are regex meta char used for char class. Since we want to match them literally we need to escape them to get \[.*\]. .* is by default greedy and will try to match as much as possible. In this case it will match abc] middle [xyz. To avoid this we make it non greedy by appending it with a ? to give \[.*?\]. Since our def of anything here actually means anything other than ] we can also use \[[^]]*?\]

EDIT:

If you want to extract words that are both inside and outside the [], you can use:

$arr = preg_split('/\[|\]/',$input);

which split the string on a [ or a ]

蝶舞 2024-09-26 17:53:42
$inside = '\[.+?\]';
$outside = '[^\[\]]+';
$or = '|';

preg_match_all(
    "~ $inside $or $outside~x", 
    "first [abc] middle [xyz] last", 
    $m);
print_r($m);

或不太冗长

  preg_match_all("~\[.+?\]|[^\[\]]+~", $str, $matches)
$inside = '\[.+?\]';
$outside = '[^\[\]]+';
$or = '|';

preg_match_all(
    "~ $inside $or $outside~x", 
    "first [abc] middle [xyz] last", 
    $m);
print_r($m);

or less verbose

  preg_match_all("~\[.+?\]|[^\[\]]+~", $str, $matches)
物价感观 2024-09-26 17:53:42

使用 preg_split 而不是 preg_match。

preg_split('/\[.*?\]/', 'first [abc] middle [xyz] last');

结果:

array(3) {
  [0]=>
  string(6) "first "
  [1]=>
  string(8) " middle "
  [2]=>
  string(5) " last"
}

ideone

Use preg_split instead of preg_match.

preg_split('/\[.*?\]/', 'first [abc] middle [xyz] last');

Result:

array(3) {
  [0]=>
  string(6) "first "
  [1]=>
  string(8) " middle "
  [2]=>
  string(5) " last"
}

ideone

软甜啾 2024-09-26 17:53:42

正如每个人都说你应该使用 preg_split,但只有一个人用满足你需求的表达方式回复,我认为这有点复杂 - 不复杂,有点冗长,但他已经更新了他的回答反驳了这一点。

这句话是大多数回复都说的。

/\[.*?\]/

但这只会打印出来,

Array
(
    [0] => first 
    [1] =>  middle 
    [2] =>  last
)

并且您声明您想要大括号内部和外部的内容,sio 更新将是:

/[\[.*?\]]/

这给您:

Array
(
    [0] => first 
    [1] => abc
    [2] =>  middle 
    [3] => xyz
    [4] =>  last
)

但正如您所看到的,它也捕获了空白,所以让我们更进一步并摆脱这些:

/[\s]*[\[.*?\]][\s]*/

这会给你一个想要的结果:

Array
(
    [0] => first
    [1] => abc
    [2] => middle
    [3] => xyz
    [4] => last
)

我认为这就是你正在寻找的表达。

这是上述正则表达式的实时演示

As every one says that you should use preg_split, but only one person replied with an expression that meets your needs, and i think that is a little complex - not complex, a little to verbose but he has updated his answer to counter that.

This expression is what most of the replies have stated.

/\[.*?\]/

But that only prints out

Array
(
    [0] => first 
    [1] =>  middle 
    [2] =>  last
)

and you stated you wanted whats inside and outside the braces, sio an update would be:

/[\[.*?\]]/

This gives you:

Array
(
    [0] => first 
    [1] => abc
    [2] =>  middle 
    [3] => xyz
    [4] =>  last
)

but as you can see that its capturing white spaces as well, so lets go a step further and get rid of those:

/[\s]*[\[.*?\]][\s]*/

This will give you a desired result:

Array
(
    [0] => first
    [1] => abc
    [2] => middle
    [3] => xyz
    [4] => last
)

This i think is the expression your looking for.

Here is a LIVE Demonstration of the above Regex

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