英文:
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 = "title of the spoiler" and body = "content"
// and it'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+(?<title>.+)\n(?<body>.+)\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+(?<title>.+)\n(?<body>.(?:.*\n)+?):::
正则表达式详细信息:
:::: 匹配起始的:::\s+: 匹配1个或多个空格spoiler: 匹配文本spoiler\s+: 匹配1个或多个空格(?<title>.+): 在名为title的捕获组中匹配1个或多个任意字符\n: 匹配一个换行符(?<body>.: 名为body的捕获组,以一个字符开始(?:.*\n)+?: 匹配0个或多个任意字符,后跟换行符,并重复此组1次或多次(懒惰模式)
)::::匹配结束的:::
英文:
You may use this regex to get correct matches:
:::\s+spoiler\s+(?<title>.+)\n(?<body>.(?:.*\n)+?):::
RegEx Details:
:::: Match starting:::\s+: Match 1+ whitespacesspoiler: Match textspoiler\s+: Match 1+ whitespaces(?<title>.+): Match 1+ of any character in capture group namedtitle\n: Match a line break(?<body>.: capture group namedbodythat 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+(?<title>\S.*)\n(?!\s*^:::)(?<body>(?:.*\n)+?):::
解释
::::字面匹配(或者如果它必须在字符串的开头,则使用^:::)\sspoiler\s+:匹配一个空格字符,然后是spoiler,后跟1个或多个空格字符(?<title>\S.*):命名组title,匹配单个非空格字符,然后是该行的其余部分\n(?!\s*^:::):匹配换行符,并断言直到行首的:::之前没有只有空格字符(?<body>(?:.*\n)+?):命名组body,重复匹配所有行,尽可能少地匹配::::字面匹配
或者对于JavaScript,其中[^]匹配包括换行符在内的任何字符:
:::\sspoiler\s+(?<title>\S.*)\n(?!\s*^:::)(?<body>[^]*?)\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+(?<title>\S.*)\n(?!\s*^:::)(?<body>(?:.*\n)+?):::
Explanation
:::Match literally (Or use^:::if it has to be at the start of the string)\sspoiler\s+Match a whitespace char, thenspoilerfollowed by 1+ whitespace chars(?<title>\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(?<body>(?:.*\n)+?)Named group body, repeat matching all lines, as few as possible:::Match literally
Or for JavaScript, where [^] matches any character including a newline:
:::\sspoiler\s+(?<title>\S.*)\n(?!\s*^:::)(?<body>[^]*?)\n:::
Note that \s can also match a newline
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论