Rob*_*ick 7 c# regex visual-studio-code
我习惯于编写支持多个选项的正则表达式来指定区分大小写、忽略空格、含义等....在 C# 中,这些选项分别用(?i),(?x)和(?s)指定。
这些修饰符如何与 Visual Studio Code 查找功能一起使用?我收到错误
正则表达式无效:组无效。
例子:
q.*?abc.*?q
Run Code Online (Sandbox Code Playgroud)
会匹配<q>heheabchihi</q>,但不匹配
<q>hehe
abchihi</q>
Run Code Online (Sandbox Code Playgroud)
由于.不匹配所有字符(\n被省略)。添加(?s)修复了 C# 正则表达式中的问题,但未修复 Visual Studio Code 中的问题。Visual Studio Code 使用正则表达式选项的方式是什么?
要匹配新行,您可以组合正则表达式选项RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
string pattern = @"q.*?abc.*?q";
string input = @"<q>hehe
abchihi</q>";
Match m = Regex.Match(input, pattern, RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline);
Console.WriteLine(m);
}
}
Run Code Online (Sandbox Code Playgroud)