php REGEX 匹配某一行之后的每一行

发布于 2024-10-20 01:35:48 字数 1112 浏览 1 评论 0原文

可能的重复:
PHP REGEX preg_math_all 特定行之后的每一行< /a>

我想要一个匹配特定行之后的每一行的模式 - 我可以依赖的唯一常量是,如果标题在那里,我知道我想要跟随行。如果我最终得到的行数比我想要的多也没关系,因为我只会取第一个,无论我需要多少行。

SURGICAL/MEDICAL HISTORY <--Header to indicate the beginning of my list
HISTORY 1 <--line1 to be matched
HISTORY 2 <--line2 to be matched
HISTORY 3 <--line3 to be matched
.
.

我不会发布文本的真实示例,因为这并不重要 - 模式必须灵活并且出于其他特殊原因。我总是对正则表达式中的行有这样的问题。这就是我所拥有的:

$ptn = "/SURGICAL\/MEDICAL HISTORY\s*(^(.+)$)+/m";

但这只匹配第一行。谢谢!

编辑:我需要将所有匹配的行分隔在一个数组中:

Array
(
    [0] => Array
        (
            [0] => SURGICAL/MEDICAL HISTORY
HISTORY 1
HISTORY 2
        )

    [1] => Array
        (
            [0] => HISTORY 1
            [1] => HISTORY 2
        )

)

Possible Duplicate:
PHP REGEX preg_math_all every line after a particular line

I want a pattern that will match every line after a particular line - the only constant that I can rely on is that if the header is there I know I want the follow lines. It doesn't matter if I end up getting more lines than I want because I'll only take the first however many I need.

SURGICAL/MEDICAL HISTORY <--Header to indicate the beginning of my list
HISTORY 1 <--line1 to be matched
HISTORY 2 <--line2 to be matched
HISTORY 3 <--line3 to be matched
.
.

I'm not posting a real sample of the text because it shouldn't matter - the pattern must be flexible and for other particular reasons. I just always have such an issue with lines in regex. Here is what I have:

$ptn = "/SURGICAL\/MEDICAL HISTORY\s*(^(.+)$)+/m";

But that only matches the first line. Thanks!

EDIT: I need all of the matched lines in separated in an array:

Array
(
    [0] => Array
        (
            [0] => SURGICAL/MEDICAL HISTORY
HISTORY 1
HISTORY 2
        )

    [1] => Array
        (
            [0] => HISTORY 1
            [1] => HISTORY 2
        )

)

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

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

发布评论

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

评论(1

ㄟ。诗瑗 2024-10-27 01:35:48

这可能有用:模式修饰符

s (PCRE_DOTALL)

<块引用>

如果设置了此修饰符,则显示一个点
模式匹配中的元字符
所有字符,包括换行符。
如果没有它,换行符将被排除。
这个修饰符相当于 Perl 的
/s 修饰符。负类如
[^a] 总是匹配换行符
性格,独立于设定
此修饰符。

您的最终正则表达式应如下所示(未经测试):

$ptn = "/SURGICAL\/MEDICAL HISTORY\s*$(.*)/ms";

This might be useful : Pattern Modifiers

s (PCRE_DOTALL)

If this modifier is set, a dot
metacharacter in the pattern matches
all characters, including newlines.
Without it, newlines are excluded.
This modifier is equivalent to Perl's
/s modifier. A negative class such as
[^a] always matches a newline
character, independent of the setting
of this modifier.

Your final regex should look like this (not tested) :

$ptn = "/SURGICAL\/MEDICAL HISTORY\s*$(.*)/ms";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文