正则表达式仅匹配最里面的分隔序列

发布于 2024-11-28 08:03:31 字数 563 浏览 6 评论 0原文

我有一个字符串,其中包含由多个字符分隔的序列:<<>>。我需要一个正则表达式来只给出最里面序列。我已经尝试过前瞻,但它们似乎没有按照我期望的方式工作。

这是一个测试字符串:

'do not match this <<but match this>> not this <<BUT NOT THIS <<this too>> IT HAS CHILDREN>> <<and <also> this>>'

它应该返回:

but match this
this too
and <also> this

正如您在第三个结果中看到的,我不能只使用 /<<[^>]+>>/ 因为字符串可以有一个分隔符字符,但不能连续有两个字符。

我刚从反复试验中走出来。在我看来这不应该这么复杂。

I have a string that contains sequences delimited by multiple characters: << and >>. I need a regular expression to only give me the innermost sequences. I have tried lookaheads but they don't seem to work in the way I expect them to.

Here is a test string:

'do not match this <<but match this>> not this <<BUT NOT THIS <<this too>> IT HAS CHILDREN>> <<and <also> this>>'

It should return:

but match this
this too
and <also> this

As you can see with the third result, I can't just use /<<[^>]+>>/ because the string may have one character of the delimiters, but not two in a row.

I'm fresh out of trial-and-error. Seems to me this shouldn't be this complicated.

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

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

发布评论

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

评论(3

白云悠悠 2024-12-05 08:03:31
@matches = $string =~ /(<<(?:(?!<<|>>).)*>>)/g;

(?:(?!PAT).)* 之于模式,正如 [^CHAR]* 之于字符。

@matches = $string =~ /(<<(?:(?!<<|>>).)*>>)/g;

(?:(?!PAT).)* is to patterns as [^CHAR]* is to characters.

乙白 2024-12-05 08:03:31
$string = 'do not match this <<but match this>> not this <<BUT NOT THIS <<this too>> IT HAS CHILDREN>> <<and <also> this>>';
@matches = $string =~ /(<<(?:[^<>]+|<(?!<)|>(?!>))*>>)/g;
$string = 'do not match this <<but match this>> not this <<BUT NOT THIS <<this too>> IT HAS CHILDREN>> <<and <also> this>>';
@matches = $string =~ /(<<(?:[^<>]+|<(?!<)|>(?!>))*>>)/g;
命硬 2024-12-05 08:03:31

这是使用 split 完成任务的方法:

my $str = 'do not match this <<but match this>> not this <<BUT NOT THIS <<this too>> IT HAS CHILDREN>> <<and <also> this>>';
my @a = split /(?=<<)/, $str;
@a = map { split /(?<=>>)/, $_ } @a;

my @match = grep { /^<<.*?>>$/ } @a;

将标签保留在那里,如果您希望将它们删除,只需执行以下操作:

@match = map { s/^<<//; s/>>$//; $_ } @match;

Here's a way to use split for the job:

my $str = 'do not match this <<but match this>> not this <<BUT NOT THIS <<this too>> IT HAS CHILDREN>> <<and <also> this>>';
my @a = split /(?=<<)/, $str;
@a = map { split /(?<=>>)/, $_ } @a;

my @match = grep { /^<<.*?>>$/ } @a;

Keeps the tags in there, if you want them removed, just do:

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