C# 为文本准备正则表达式
我有一段文字:C# 为文本准备正则表达式,c#,regex,C#,Regex,我有一段文字: "text 1 Sentence 1."*"the (1) /ðiː/ Sentence 1 Translated." "text 2 Sentence 2."*"of (2) /əv/ Sentence 2 Translated." "Text 3 Sentence 3!"*"and (3) /ænd/ Sentence 3 Translated!" 如何从“到最近的” 把它们都收集起来 "text 1 Sentence 1."*"the (1) /ði
"text 1
Sentence 1."*"the (1)
/ðiː/
Sentence 1 Translated."
"text 2
Sentence 2."*"of (2)
/əv/
Sentence 2 Translated."
"Text 3
Sentence 3!"*"and (3)
/ænd/
Sentence 3 Translated!"
如何从“
到最近的”
把它们都收集起来
"text 1
Sentence 1."*"the (1)
/ðiː/
Sentence 1 Translated."
我尝试过这个,但没有成功:
"(.*?)""
根据示例字符串判断,您需要从一行的第一个字符
“
到结束一行的第一个”
中获取子字符串
如果文本位于单个变量内,例如text
,则可以使用
var results = Regex.Matches(text, @"(?sm)^("".*?"")\r?$")
.Cast<Match>()
.Select(m => m.Groups[1].Value);
var results=Regex.Matches(文本,@“(?sm)^(“*?”)\r?$”)
.Cast()
.Select(m=>m.Groups[1].Value);
见
图案细节
-(?sm)
和RegexOptions。单线
处于启用状态RegexOptions。多线
-行的开头^
-第1组:(“.*”)
,任何0+字符,但尽可能少,以及一个”
“
-可选CR(因为\r?
在CR之前不匹配)$
-行结束$
var results=Regex.Matches(text,@“(?sm)^(“*?”)\r?$”).Cast().Select(m=>m.Groups[1].Value)代码>。或者,如果必须省略引号,@“(?sm)^”“(.*?”\r?$”
您可以使用此网站测试您的regexp: