匹配多行,这些行不在“< p>”开头不使用“ m&quot”修饰符(Golang味)

发布于 2025-02-12 03:08:51 字数 1190 浏览 2 评论 0原文

我试图匹配多个不以HTML < p>标记的方式匹配,仅使用g修饰符带有Regex的Golang风味。

以下是一个例子:

Lorem ipsum

<p><span class="slugline">INNEN. Wohnung, Erdgeschoss – Tag</span></p><br>

Dolor sit amet

1234

<p><span class="slugline">INNEN. Wieslers Wohnung, Fahrstuhl – Tag</span></p><br>

Et respice finem

<p><span class="slugline">AUSSEN. Wohnung - Nacht</span></p><br>

<p><span class="slugline">INNEN. Wohnung, Erdgeschoss – Tag</span></p><br>

<p><span class="charleft">Maik</span><span class="dialogleft">(leise) Hallo.</span></p>

Quod erat demonstrandum

正则应匹配以:

  • lorem ipsum dolor
  • sit amet
  • 1234
  • et Respice Finem
  • Quod erat excliveandum,

使用Golang味的mg修饰符很容易: ^([^&lt; \ n \ r] |&lt;([^p] | $))。

但是我正在寻找一个正则表达式,该表达式在没有m修饰符的情况下工作。我不能仅适用于g修饰符。

I'm trying to match multiple lines that do not begin with a HTML <p> tag, using just the g modifier with the Golang flavor of RegEx.

Here's an example:

Lorem ipsum

<p><span class="slugline">INNEN. Wohnung, Erdgeschoss – Tag</span></p><br>

Dolor sit amet

1234

<p><span class="slugline">INNEN. Wieslers Wohnung, Fahrstuhl – Tag</span></p><br>

Et respice finem

<p><span class="slugline">AUSSEN. Wohnung - Nacht</span></p><br>

<p><span class="slugline">INNEN. Wohnung, Erdgeschoss – Tag</span></p><br>

<p><span class="charleft">Maik</span><span class="dialogleft">(leise) Hallo.</span></p>

Quod erat demonstrandum

The regex should match the lines and paragraphs that begin with:

  • Lorem ipsum
  • Dolor sit amet
  • 1234
  • Et respice finem
  • Quod erat demonstrandum

It's easy with the mg modifiers of the Golang flavor: ^([^<\n\r]|<([^p]|$)).*

But I'm looking for a regular expression that works without the m modifier. I can't make it work with just the g modifier.

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

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

发布评论

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

评论(1

眼睛会笑 2025-02-19 03:08:51

与其匹配要保留的内容,您可以匹配自己不需要的东西,并将其用作模式与 split 字符串。

如果您的文本位于变量s中,则可以继续这样做:

    para := regexp.MustCompile("[\n\r]+(<p>.*[\n\r]*)*")
    lines := para.Split(s, -1)
    for _, line := range lines {
        fmt.Println(line)
    }

然后将输出:

Lorem ipsum
Dolor sit amet
1234
Et respice finem
Quod erat demonstrandum

Instead of matching what you want to keep, you could match what you don't want, and use that as pattern to split the string.

If your text sits in variable s, you could continue like this:

    para := regexp.MustCompile("[\n\r]+(<p>.*[\n\r]*)*")
    lines := para.Split(s, -1)
    for _, line := range lines {
        fmt.Println(line)
    }

This will then output:

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