Searching String or StringBuilder with a pattern range /start/ /end/

Viewed 154

I want to create a function (w/ a set of helper functions if needed) in C# that would perform a similar thing as awk '/start/,/end/' file - except that it would include all the last matches, rather than terminating on the first.

Lets say we have:

# 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"

Expected:

"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 output:

# 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"

I initially thought that I might just get a regex match by two conditions pattern_1 | pattern_2 however this will not work if there are values in between the matching values.

I also discovered that C# StringBuilder class does not have .indexOf() and .lastIndexOf() methods (I have a bit more experience in JAVA, so was thinking to use these untill I saw C# not having them). Since I don't have these methods and would need to possibly implement them, I wanted to ask if this is the approach to go? This section even suggests to use String if extensive searching is needed: MSDN - and I can certainly use that as well. I chose to use StringBuilder because the string concatenation is performed constantly, should I use stringbuilder type when building the string (a lot of concatenation), but then convert to string type when searching?

I would also like this to be performant and it would be awesome to hear suggestions how to make it as such. General guidance and implementation details are appreciated.

1 Answers
Related