gpt4 book ai didi

regex - 如何在积极回顾后排除事件?

转载 作者:行者123 更新时间:2023-12-05 03:34:09 29 4
gpt4 key购买 nike

假设我有以下 markdown列表项:

- [x] Example of a completed task.
- [x] ! Example of a completed task.
- [x] ? Example of a completed task.

我有兴趣使用 regex 解析该项目并提取以下组捕获:

  • $1 : 左边[和右边]符号 x 时的括号介于两者之间
  • $2 : 符号 x在括号之间 []
  • $3 : 修饰符 ![x] 之后
  • $4 : 修饰符 ?[x] 之后
  • $5 : [x] 之后的文字没有修饰符,例如 [x] This is targeted.
  • $6 : [x] ! 之后的文字
  • $7 : [x] ? 之后的文字

在使用在线解析器进行大量反复试验之后,我得出了以下结论:

((?<=x)\]|\[(?=x]))|((?<=\[)x(?=\]))|((?<=\[x\]\s)!(?=\s))|((?<=\[x\]\s)\?(?=\s))|((?<=\[x\]\s)[^!?].*)|((?<=\[x\]\s!\s).*)|((?<=\[x\]\s\?\s).*)

制作regex上面更具可读性,这些是一一列出的捕获组:

  • $1 : ((?<=x)\]|\[(?=x]))
  • $2 : ((?<=\[)x(?=\]))
  • $3 : ((?<=\[x\]\s)!(?=\s))
  • $4 : ((?<=\[x\]\s)\?(?=\s))
  • $5 : ((?<=\[x\]\s)[^!?].*)
  • $6 : ((?<=\[x\]\s!\s).*)
  • $7 : ((?<=\[x\]\s\?\s).*)

这很可能不是最好的方法,但至少它似乎捕捉到了我想要的东西:

Matches for the example list items

我想扩展 regex捕获 markdown 中的行表那个看起来像这样:

|       | Task name                               |    Plan     |   Actual    |      File      |
| :---- | :-------------------------------------- | :---------: | :---------: | :------------: |
| [x] | Task one with a reasonably long name. | 08:00-08:45 | 08:00-09:00 | [[task-one]] |
| [x] ! | Task two with a reasonably long name. | 09:00-09:30 | | [[task-two]] |
| [x] ? | Task three with a reasonably long name. | 11:00-13:00 | | [[task-three]] |

更具体地说,我对与上面相同的组捕获感兴趣,但我想排除表格网格(即 | )。所以,团体$1$4应该保持不变,但组 $5$7应捕获文本,不包括 | ,例如,如下面的选择:

Matches for the example table

您对我如何调整有任何想法吗,例如,组 $5 的正则表达式排除 | .我无休止地尝试了各种否定(例如 [^\|] )。我正在使用 Oniguruma regular expressions .

最佳答案

灵感来自 Wiktor 的答案, 检查下面的正则表达式,它很短

(?:\G(?<!\A)\||(?:\[x]\s[?!]?\s*\|?))\K([^|\n]*)

上面的解释

1.\G(?!\A)\|

\G asserts position at the end of the previous match or the start of the string for the first match. Negative Lookbehind (?!\A)

  1. \A asserts position at start of the string
  2. | matches the character |
  1. (?:\[x]\s[?!]?\s*\|?)

Non-capturing group. That matches [x], \s (space), [?|!] (zero or 1) followed by \s* (zero or more) and a | (zero or one)

  1. \K

\K resets the starting point of the reported match.

  1. ([^|\n]*)

All characters except | or \n (newline) matches previous token zero or unlimited times.

关于regex - 如何在积极回顾后排除事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70218387/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com