匹配 lemmy 剧透标签的正则表达式

huangapple go评论70阅读模式
英文:

Regex for matching lemmy spoiler tags

问题

:::\sspoiler\s+(?<title>[^\n]+)\n(?<body>[^\n]+)\n:::
英文:

I'm trying to write regex (JS flavor) that matches spoilers in text.

// this should match with group title = &quot;title of the spoiler&quot; and body = &quot;content&quot;
// and it&#39;s only one working for now :/

::: spoiler title of the spoiler
content
:::

// this should match

::: spoiler title
content
on multiple
lines

**with some style**
:::

// this should match

::: spoiler title
body
containing
colon:
:::

Here is what I currently have:

:::\sspoiler\s+(?&lt;title&gt;.+)\n(?&lt;body&gt;.+)\n:::

However it doesn't match spoilers containing newlines, only first example is working.

link to regex101.com

Thanks in advance.

答案1

得分: 1

您可以使用以下正则表达式来获取正确的匹配:

:::\s+spoiler\s+(?&lt;title&gt;.+)\n(?&lt;body&gt;.(?:.*\n)+?):::

更新的正则表达式演示

正则表达式详细信息:

  • :::: 匹配起始的 :::
  • \s+: 匹配1个或多个空格
  • spoiler: 匹配文本 spoiler
  • \s+: 匹配1个或多个空格
  • (?&lt;title&gt;.+): 在名为 title 的捕获组中匹配1个或多个任意字符
  • \n: 匹配一个换行符
  • (?&lt;body&gt;.: 名为 body 的捕获组,以一个字符开始
    • (?:.*\n)+?: 匹配0个或多个任意字符,后跟换行符,并重复此组1次或多次(懒惰模式)
  • ):
  • ::: 匹配结束的 :::
英文:

You may use this regex to get correct matches:

:::\s+spoiler\s+(?&lt;title&gt;.+)\n(?&lt;body&gt;.(?:.*\n)+?):::

Updated RegEx Demo

RegEx Details:

  • :::: Match starting :::
  • \s+: Match 1+ whitespaces
  • spoiler: Match text spoiler
  • \s+: Match 1+ whitespaces
  • (?&lt;title&gt;.+): Match 1+ of any character in capture group named title
  • \n: Match a line break
  • (?&lt;body&gt;.: capture group named body that starts with a character
    • (?:.*\n)+?: Match 0 or of any character followed by a line break and repeat this group 1+ times (lazy)
  • ):
  • ::: Match ending :::

答案2

得分: 0

您可以断言,在匹配标题的单个非空格字符后,下一行直到匹配行首的:::之前不能只包含空格字符。

:::\sspoiler\s+(?&lt;title&gt;\S.*)\n(?!\s*^:::)(?&lt;body&gt;(?:.*\n)+?):::

解释

  • ::::字面匹配(或者如果它必须在字符串的开头,则使用^:::
  • \sspoiler\s+:匹配一个空格字符,然后是spoiler,后跟1个或多个空格字符
  • (?&lt;title&gt;\S.*):命名组title,匹配单个非空格字符,然后是该行的其余部分
  • \n(?!\s*^:::):匹配换行符,并断言直到行首的:::之前没有只有空格字符
  • (?&lt;body&gt;(?:.*\n)+?):命名组body,重复匹配所有行,尽可能少地匹配
  • ::::字面匹配

正则表达式演示

或者对于JavaScript,其中[^]匹配包括换行符在内的任何字符:

:::\sspoiler\s+(?&lt;title&gt;\S.*)\n(?!\s*^:::)(?&lt;body&gt;[^]*?)\n:::

正则表达式演示

注意\s也可以匹配换行符。

英文:

You could assert that after matching a single non whitespace char for the title, the next lines do not have only whitespace chars until matching ::: at the start of a line.

:::\sspoiler\s+(?&lt;title&gt;\S.*)\n(?!\s*^:::)(?&lt;body&gt;(?:.*\n)+?):::

Explanation

  • ::: Match literally (Or use ^::: if it has to be at the start of the string)
  • \sspoiler\s+ Match a whitespace char, then spoiler followed by 1+ whitespace chars
  • (?&lt;title&gt;\S.*) Named group title, match a single non whitespace char and then the rest of the line
  • \n(?!\s*^:::) Match a newline, and assert not only whitespace chars until ::: at the start of a line
  • (?&lt;body&gt;(?:.*\n)+?) Named group body, repeat matching all lines, as few as possible
  • ::: Match literally

Regex demo

Or for JavaScript, where [^] matches any character including a newline:

:::\sspoiler\s+(?&lt;title&gt;\S.*)\n(?!\s*^:::)(?&lt;body&gt;[^]*?)\n:::

Regex demo

Note that \s can also match a newline

huangapple
  • 本文由 发表于 2023年7月13日 23:22:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76681034.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定