如何在 3 个重音符之间匹配降价块代码

问题描述

  • 我知道这是一个常见问题,但我搜索了很多但没有找到我需要的
  • 我正在尝试创建简单的降价插件,但我无法匹配块代码我尝试过这个正则表达式 /(^\`{3}[\w|\W]+\`{3})/gm 但它让我知道第一个和最后一个重音符之间的所有内容,如果有另一个代码,它会将它们全部添加一个
  • 例如下面的字符串
 ``` 
  block code 
  word1
  word2
 ```

word without grave accent and it is will match it too

 ```
  another block code
  word1
  word2
  - it's will match it as part of the first block code
 ```

"""
some text after the grave accent text
word1
word2
"""`
  • 这个正则表达式的结果就像下面的数组
0: "``` \nblock code\nword1\nword2\n```\n\nword without grave accent and it's will match it too\n\n```\nanother block code\nword1\nword2\n- it's will match it as part of the first block code\n```"
length: 1
  • 我希望它像这样匹配
0: "``` \nblock code\nword1\nword2\n```"
1: "```\nanother block code\nword1\nword2\n- it's will match it as part of the first block code\n```"
length: 2
  • 我不太擅长正则表达式,所以我希望有人帮助我

解决方法

您需要懒惰地匹配您的 (\`{3}[\w|\W]+?\`{3}) 量词:+?
\`{3} 将尝试用尽可能少的字符匹配一次或多次,确保它不会贪婪地占用 {{1}} 之间的所有内容。默认情况下,量词是贪婪的。

regex101 example

这里有一些关于懒惰与贪婪的more reading

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...