Visual Studio Code 中的多行正则表达式 [英] Multi-line regular expressions in Visual Studio Code

查看:203
本文介绍了Visual Studio Code 中的多行正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想不出一种方法让正则表达式匹配停止不是在行尾,而是在 VS Code 的文件尾?是工具限制还是存在某种我不知道的模式?

I cannot figure a way to make regular expression match stop not on end of line, but on end of file in VS Code? Is it a tool limitation or there is some kind of pattern that I am not aware of?

推荐答案

似乎 CR 与 [\s\S] 不匹配.将 \r 添加到这个字符类:

It seems the CR is not matched with [\s\S]. Add \r to this character class:

[\s\S\r]+

将匹配任何 1+ 个字符.

will match any 1+ chars.

其他证明有效的替代方法是 [^\r]+[\w\W]+.

Other alternatives that proved working are [^\r]+ and [\w\W]+.

如果要让任何字符类匹配换行符,无论是正字符类还是负字符类,都需要在其中添加\r.

If you want to make any character class match line breaks, be it a positive or negative character class, you need to add \r in it.

示例:

  • 两个最接近的 ab 字符之间的任何文本:a[^ab\r]*b
  • START 和最近的 STOP 字之间的任何文本:
    • START[\s\S\r]*?STOP
    • START[^\r]*?STOP
    • START[\w\W]*?STOP
    • Any text between the two closest a and b chars: a[^ab\r]*b
    • Any text between START and the closest STOP words:
      • START[\s\S\r]*?STOP
      • START[^\r]*?STOP
      • START[\w\W]*?STOP
      • START(?:(?!START)[\s\S\r])*?STOP

      查看下面的演示屏幕截图:

      See a demo screenshot below:

      这篇关于Visual Studio Code 中的多行正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆