字符串中的正则表达式顶级内容 [英] regex top level contents from a string

查看:64
本文介绍了字符串中的正则表达式顶级内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮忙,我的正则表达能力使我失望.我有以下字符串:

Please help, my regular expression skills fail me. I have the following string:

username|email_address|phone_numbers[number]profile[title|addresses[id]]

我希望能够提取方括号之间的任何数据,但不能提取该数据是已提取的集合的子集的位置.因此,任何嵌套都应作为父级提取的字符串的一部分保留.

I want to be able to extract any data between square brackets, but not where that data is a subset of an already extracted set. So any nestings should be left as part of the parent's extracted string.

在上面的示例中,我将提取两个部分:

In the above example I'd have extracted two parts:

"number"
"title|addresses[id]"

请注意[id]是如何提取的,因为它是较低级别数据集的一部分.

Note how the [id] isn't extracted as it's part of a lower level dataset.

我一直在尝试使用preg_match进行此操作,但是我可能不得不求助于字符串中的每个字符.

I've been attempting to do this with preg_match, but think I may have to resort to iterating over each character in the string.

推荐答案

以下是正则表达式解决方案:

Here's a regex solution:

preg_match_all(
    '/(?<=\[)     # Assert that the previous characters is a [
      (?:         # Match either...
       [^[\]]*    # any number of characters except brackets
      |           # or
       \[         # an opening bracket
       (?R)       # containing a match of this very regex
       \]         # followed by a closing bracket
      )*          # Repeat as needed
      (?=\])      # Assert the next character is a ]/x', 
    $subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];

这篇关于字符串中的正则表达式顶级内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆