Reputation: 3
Currently working on a string in C#.
Example:
07.02.2011 17:24:17 [/sbc_DIG] [ERROR] CommandExecutionService:290 - Error during command execution
I can't split using spaces as in somecase there will spaces inside squarebracet
In this case I'm interested in getting 07.02.2011
, 17:24:17
and CommandExecutionService:290
using regex
Solution based on Ikegami answer
@"^(?[0-9.]+) (?[0-9:]+) [.?] [.?] (?[a-zA-Z0-9:]+)\s";
Upvotes: 0
Views: 127
Reputation: 385655
Using Perl syntax, but the regex pattern should be the same in C#,
my ($date, $time, $pos) = $line =~ /
^
(\S+) \s+
(\S+) \s+
\[ [^\]]* \] \s+
\[ [^\]]* \] \s+
(\S+)
/x;
Upvotes: 0
Reputation: 56390
Why not just split on spaces for the first four fields and then take the first word of the last field?
It looks like your data is really structured like:
<date>(space)<time>(space)<URL?>(space)<severity/log level>(space)<message>
So just split it into those components by splitting on spaces:
string[] fields = myString.split(" ", 4);
Upvotes: 2