排除正则表达式结果中字符串的第一个和最后一个引号

时间:2016-04-12 03:45:24

标签: c# regex

我正在运行一个小小的c#程序,我需要从字符串中提取转义引用的单词。

来自linqpad的示例代码:

string s = "action = 0;\r\ndir = \"C:\\\\folder\\\\\";\r\nresult";
var pattern = "\".*?\"";
var result = Regex.Split(s, pattern);
result.Dump();

输入(实际输入包含更多转义的偶数引号)

"action = 0;\r\ndir = \"C:\\\\folder\\\\\";\r\nresult"

预期结果

"C:\\folder\\"

实际结果(2项)

"action = 0;
dir =  "
_____
";
result"

我与我的要求完全相反。如何使正则表达式忽略实际字符串的起始(和结束)引用?为什么它包含在搜索中?我使用了来自similar SO问题的正则表达式,但仍然没有得到预期的结果。我只想用转义引号过滤。

2 个答案:

答案 0 :(得分:2)

不要使用Regex.Split,请尝试Regex.Match

答案 1 :(得分:0)

您不需要RegEx。只需使用quantity+= inputFile.nextInt();,第二个数组元素就可以获得所需的路径。然后,您可以package ExceptionsActivity; public class Exceptions { public static void f1(int x) throws XisFiveException{ if (x == 5){ throw new XisFiveException("X cannot be 5"); } else { System.out.println("Success, x = "); } } public static void f2_1(int x) throws XisFiveException{ try { f1(x); } catch (XisFiveException e) { f1(x + 1); } } public static void f2_2(int x) throws XisFiveException{ } public static void main(String[] args) { } } 删除引号,cell.detaillabel.text = @"#my#name#is#umesh#verma"; 删除$date = new DateTime(); if($date->format('w') != 0) { $date->modify('last Sunday of last month'); } 部分。类似的东西:

String.Split(';')
相关问题