[英]C# Regex not returning
我正在研究一個練習問題,它給了我更長的琴弦。 我需要將字符串從引號中拉出(wh
// names is a string[] composed of names like these: "BOB", "JOE", "FRANK", etc...
// (the ""s are part of the string, not string designations). I suppose that makes them "\"Bob\"", etc...
foreach(string name in names)
{
Regex regex = new Regex("/\"(.+)\"/");
Match match = regex.Match(name);
Console.WriteLine (match.Value);
if (match.Success)
{
Console.WriteLine("string: {0} and match value: {1}", name, match.Groups[1].Value);
}
}
我什么都沒注銷。 我嘗試了幾種引用.Value
方法,但是我也無法記錄普通字符串,因此我的Regex沒有任何匹配項。 我也遵循了幾個示例。
Regex101告訴我應該匹配得很好,所以我的C#實現中必須有一些錯誤。 但我不知道。 我只需要有人讓我挺直。 謝謝!
刪除正則表達式中的正斜杠。 它們用於指示某些語言或格式的正則表達式的開始和結束,而通過Regex
類創建正則表達式時則不需要。
Regex regex = new Regex("\"(.+)\"");
結果:
“ BOB”
字符串:“ BOB”,匹配值:BOB
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.