Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为文本准备正则表达式_C#_Regex - Fatal编程技术网

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+字符,但尽可能少,以及一个
  • \r?
    -可选CR(因为
    $
    在CR之前不匹配)
  • $
    -行结束

Try
var results=Regex.Matches(text,@“(?sm)^(“*?”)\r?$”).Cast().Select(m=>m.Groups[1].Value)。或者,如果必须省略引号,
@“(?sm)^”“(.*?”\r?$”
您可以使用此网站测试您的regexp: