我想在C#中创建一个函数(W /一套辅助功能,如果需要),将执行类似的事情awk '/start/,/end/' file
- 除了它会包括所有最后的比赛,而不是终止在第一场比赛。搜索字符串或StringBuilder与模式范围/开始/ /结束/
比方说,我们有:
# cat text
"13:08:30:5276604 Main: 41044 - 48.7617 M-- Other PIDS 2 - 79.1016 M"
"13:08:30:5736962 Main: 41044 - 48.7617 M-- Other PIDS 2 - 79.1016 M"
"13:08:30:6227343 Main: 41044 - 48.7617 M-- Other PIDS 2 - 79.1016 M"
"13:08:30:6757752 Main: 41044 - 48.7617 M-- Other PIDS 2 - 79.1016 M"
"13:08:30:7208103 Main: 41044 - 48.7617 M-- Other PIDS 2 - 79.1016 M"
"13:08:30:7668739 Main: 41044 - 48.7617 M-- Other PIDS 2 - 79.1016 M"
"13:08:30:8129079 Main: 41044 - 48.7617 M-- Other PIDS 2 - 79.1016 M"
预计:
"13:08:30:6227343 Main: 41044 - 48.7617 M-- Other PIDS 2 - 79.1016 M"
"13:08:30:6757752 Main: 41044 - 48.7617 M-- Other PIDS 2 - 79.1016 M"
"13:08:30:7208103 Main: 41044 - 48.7617 M-- Other PIDS 2 - 79.1016 M"
"13:08:30:7668739 Main: 41044 - 48.7617 M-- Other PIDS 2 - 79.1016 M"
AWK输出:
# awk '/13:08:30:62/,/13:08:30:7/' text
"13:08:30:6227343 Main: 41044 - 48.7617 M-- Other PIDS 2 - 79.1016 M"
"13:08:30:6757752 Main: 41044 - 48.7617 M-- Other PIDS 2 - 79.1016 M"
"13:08:30:7208103 Main: 41044 - 48.7617 M-- Other PIDS 2 - 79.1016 M"
我最初以为我可能只是由两个条件pattern_1 | pattern_2
得到一个正则表达式匹配然而,如果匹配值之间有值,这将不起作用。我还发现C#StringBuilder类没有.indexOf()
和.lastIndexOf()
方法(我在JAVA方面有更多的经验,所以想用这些直到我看到C#没有它们)。由于我没有这些方法,并且需要执行这些方法,所以我想问一下这是否可行?如果需要大量的搜索,本节甚至建议使用String:MSDN - 我当然也可以使用它。我选择使用StringBuilder是因为字符串连续不断地执行,当我在构建字符串(很多并置)时使用stringbuilder
类型,但是在搜索时转换为string
类型?
我也希望这是高性能的,听到建议如何使它成为可能会令人敬畏。一般指导和实施细节表示赞赏。
具体怎么是你的问题?这应该只适用于这种特殊情况,还是应该与awk在同一范围内工作? – MetaColon
'StringBuilder'用于_building_字符串。首先构建它,然后生成整个字符串并使用'String'函数进行搜索。 –
@MetaColon - 比一般的特例更具普遍性。我将其指定为:“给定一个utf8字符串中存在的两个模式,找到以第一个模式开始并以第二个模式结束的文本。”可以添加包含/ end/pattern的整行的额外要求。 @D赤柱 - 得到确认你是在正确的轨道上总是很好,谢谢。 –