我正在嘗試構建一個正則運算式,當一行等于“--- admonition”時停止。
例如,我有:
??? ad-question Quels sont les deux types de bornages ?
Il y en a deux :
- Le bornage amiable.
- Le bornage judiciaire.
test
--- admonition
我可以在一個頁面上多次使用相同的捕獲格式。
我想在第一組中檢索(在每場比賽中):
Quels sont les deux types de Bornages ?
在一秒鐘內:
Il y en a deux :
Le Bornage 和藹可親。
出生司法。
測驗
我試過 :
^\?{3} ad-question {1}(. )\n*((?:\n(?:^[^#].{0,2}$|^[^#].{3}(?<!---).*)) )
或者
^\?{3} ad-question {1}(. )\n*((?:\n(?:^[^\n#].{0,2}$|^[^\n#](?<!----).*)) )
但它并沒有停留在“\n--- admonition”,而是在兩組之間換了新的線。
有人可以幫我建立這個正則運算式嗎?
ps:我必須在兩組之間以及組2和“----告誡”之間換行。因此,必須避免在組中使用這些行。
謝謝你的幫助。
uj5u.com熱心網友回復:
如果您想要 2 個捕獲組而不匹配組之間的換行符,但組之間必須至少有一個完整的空行:
^\?{3} ad-question (. )\n{2,}((?:(?!---).*\n)*?)\n ---
模式匹配:
^字串的開始\?{3} ad-question比賽??? ad-question(. )捕獲組1,匹配整行\n{2,}匹配 2 個或多個換行符,以便其間至少有一個空行(捕獲組 2(?:(?!---).*\n)*?盡可能重復匹配所有不以 --- 開頭的行和換行符
)關閉組 2\n ---匹配 1 個或多個換行符和---
正則運算式演示
如果應該至少存在一個換行符:
^\?{3} ad-question (. )\n ((?:(?!---).*\n)*?)\n*---
正則運算式演示
uj5u.com熱心網友回復:
試試這個正則運算式:
\?{3}\s*(. )\s*((?:(?!-{3} admonition)[\s\S])*?)\s*-{3} admonition
點擊演示
解釋:
\?{3}- 匹配 3 次出現?\s*- 匹配 0 個或多個空格(. )- 匹配除新行之外的任何字符的 1 次或多次出現,并將其捕獲在第 1 組中\s*- 匹配 0 個或多個空格((?:(?!-{3} admonition)[\s\S])*?)\s*-{3} admonition- 匹配 0 次或多次出現的不以 . 開頭的任何字符--- admonition。匹配所有此類字符后,它匹配 0 個或多個全空格,后跟單詞--- admonition
uj5u.com熱心網友回復:
我猜想這樣做有很多方法;我的兩分錢:
^\?{3}\h ad-question\h (. )\n ((?:.*\n?) ?)\n ^---\h admonition$
查看在線演示
^\?{3}\h ad-question\h- 起始行錨,后跟三個文字問號,1 (貪婪)水平空白字符和字面上的“廣告問題”和另外 1 空白字符;(. )- 您的第一個捕獲組,除換行符外有 1 (貪婪)字符;\n- 1 (貪婪)換行符。((?:.*\n?) ?)- 具有嵌套非捕獲組的第二個捕獲組匹配 1 (惰性)次,捕獲 0 個字符,直到可選的換行符;\n- 1 (貪婪)換行符。^---\h admonition$- 從起始線錨點到結束線錨點,匹配:'---'、多個空白字符和'警告'。
uj5u.com熱心網友回復:
你很可能需要重新。DOTALL和重新。多行標志。您還可以將其用作模式中的行內標志:'(?s)' and '(?m)'.
DOTALL還可以'.'捕獲'\n'它通常不匹配的內容(re.DOTALL 是 python - 其他方言具有類似的標志,fe: JS,Java)。
您可以使用r'\?\?\?(.*?)\?(.*?)--- admonition'這兩個標志來捕獲您的標志。
Python 示例(JS 有 DOTALL
import re
text = """??? ad-question Quels sont les deux types de bornages ?
Il y en a deux :
- Le bornage amiable.
- Le bornage judiciaire.
test
--- admonition
??? ad-question 2 types de bornages ?
Il y en a deux :
- Le bornage judiciaire.
test 2
--- admonition"""
pattern = r'\?\?\?(.*?)\?(.*?)--- admonition'
for f in re.finditer(pattern, text, re.MULTILINE | re.DOTALL):
print(f)
print(f.groups()) # tuple of groups (A, B, ..) of grouped matches
輸出:
<re.Match object; span=(0, 144), match='??? ad-question Quels sont les deux types de born>
(' ad-question Quels sont les deux types de bornages ',
'\n\nIl y en a deux :\n\n- Le bornage amiable.\n\n- Le bornage judiciaire.\n\ntest\n\n')
<re.Match object; span=(145, 251), match='??? ad-question 2 types de bornages ?\n\nIl y en>
(' ad-question 2 types de bornages ',
'\n\nIl y en a deux :\n\n- Le bornage judiciaire.\n\ntest 2\n\n')
模式'\?\?\?(.*?)\?(.*?)--- admonition'解釋:
\?\?\? - 3 literal question marks (QM)
(.*?)\? - non greedy capture (including \n) up to 1st QM
(.*?)--- admonition - non greedy capture up to ---admonition
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/411023.html
標籤:
上一篇:如何使用sed僅輸出捕獲組
下一篇:以下運算式的正則運算式
