正则表达式 – 用于编写长正则表达式,嵌入式注释的习语?

前端之家收集整理的这篇文章主要介绍了正则表达式 – 用于编写长正则表达式,嵌入式注释的习语?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有些语言具有在长正则表达式中嵌入换行符和空格的功能,以使它们更具可读性
  1. ( yogi | booboo ) # match something
  2. \s
  3. ( the \s)? # optional article
  4. bear # bears are not Mr. Ranger

AFAICT golang没有那个选项,是吗?

缺乏这一点,composed regex是明确的唯一选择吗?还是有另一个成语?我现在没有找到任何长regexen的例子.

大多数时候,人们只是提供评论,其中描述了正则表达式匹配的内容.但是浏览Go源代码我发现了 this个有趣的例子:
  1. // removeRE is the list of patterns to skip over at the beginning of a
  2. // message when looking for message text.
  3. var removeRE = regexp.MustCompile(`(?m-s)\A(` +
  4. // Skip leading "Hello so-and-so," generated by codereview plugin.
  5. `(Hello(.|\n)*?\n\n)` +
  6.  
  7. // Skip quoted text.
  8. `|((On.*|.* writes|.* wrote):\n)` +
  9. `|((>.*\n)+)` +
  10.  
  11. // Skip lines with no letters.
  12. `|(([^A-Za-z]*\n)+)` +
  13.  
  14. // Skip links to comments and file info.
  15. `|(http://codereview.*\n([^ ]+:[0-9]+:.*\n)?)` +
  16. `|(File .*:\n)` +
  17.  
  18. `)`,)

猜你在找的正则表达式相关文章