目标:
使正则表达式代码在 C# 代码中工作
问题:
为了使正则表达式代码在 C# 中工作,我错过了什么语法。 来自 https://regexr.com/578ul 的正则表达式代码在 onelinegdb (https://www.onlinegdb.com/HycbSKxAL( 上不起作用
我有一个符号"'是正则表达式代码的一部分,但 csharp 认为它是 c# 的一部分,而不是符号。
<小时 />正则表达式
,["0x465a27d8333756e1:0x66460f22856aea3b","broadway 22, 123 45 ny",null,[null,null,55.0359401,13.9717872]
,0,1]
C# 代码
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"","broad.*]";
string input = @",["0x465a27d8333756e1:0x66460f22856aea3b","broadway 22, 123 45 ny",null,[null,null,55.0359401,13.9717872]
,0,1]"
foreach (Match m in Regex.Matches(input, pattern))
{
Console.WriteLine(m.Value);
}
}
}
正则表达式:
https://regexr.com/578ul
Onlinegdb with C# code:
https://www.onlinegdb.com/HycbSKxAL
谢谢!
要转义"
逐字字符串中的字符,您可以使用另一个"
字符,因此每次出现""
都会转换为单个"
字符:
var str = @""""; // string consisting of one "
所以你的模式将看起来像这样:
string pattern = @""",""broad.*]";
和输入:
string input = @",[""0x465a27d8333756e1:0x66460f22856aea3b"",""broadway 22, 123 45 ny"",null,[null,null,55.0359401,13.9717872]
,0,1]";
但在这种特殊情况下,使用带有转义的简单字符串似乎会更容易:
string pattern = "","broad.*]";