c# - 为文本准备正则表达式
问题描述
我有一段文字:
"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);
查看正则表达式演示
图案细节
(?sm)
-RegexOptions.Singleline
并且RegexOptions.Multiline
在^
- 一行的开始("".*?"")
- 第 1 组:"
,任何 0+ 个字符,但尽可能少,以及"
\r?
- 一个可选的 CR(因为$
在 CR 之前不匹配)$
- 行结束。
推荐阅读
- c# - 如何保持不同的线程有自己的变量计算?
- vba - 使用 VBA 在 MS Word 中进行条件格式设置
- c# - Can Radio Buttons be used in Xamarin.Forms?
- ios - 使用 Combine 发布 API 请求时遇到问题
- python - How to write to a directory os agnostic?
- c++ - 在子文件夹中编译 .h 和 .cpp 时的 C++ 未定义参考
- c# - c# itext7/itextsharp : 如何在 PDF 文件中查找特定术语的坐标?
- kofax - KTA API - 如何使用 SDK 按文档 ID 获取图像文件?
- r - 如何将 R 中的数据表表示为热图
- ios - 当应用程序终止时,将推送通知数据从通知服务扩展保存到核心数据