Closed GoogleCodeExporter closed 9 years ago
the problem seems to lie on the ";" delimiter.
the code
string infoline =
"AC=2;AF=0.020;AN=100;DP=2575;Dels=0.00;HRun=0;MQ0=0;SB=-378.92;set=Intersection
";
RE2 pattern("DP=(.*);");
string match;
RE2::PartialMatch(infoline, pattern, &match);
takes everything from "DP=" which is unique in the string to the last of ";"
which is not unique in the string.
resulting in the output
the match here has been =
2575;Dels=0.00;HRun=0;MQ0=0;SB=-378.92;set=Intersection
I tried also with
RE2 pattern("DP=(.*);");
StringPiece input(infoline);
string match;
RE2::Consume(&input, pattern, &match);
but without success.
what is the best way to have the match up to the first ";" delimiter only?
Original comment by francesc...@gmail.com
on 29 May 2012 at 2:38
I was going to suggest the ; as the problem but in your first example SB= only
has one ; after it, so that didn't seem like it could be the problem.
In any event, you should probably write "DP=([^;]*)" as the expression. That
will match as much as possible after the = but stop at the first ; or at the
end of the string, so it will handle the last field too. Also, if a field name
could be a suffix of another field name (for example if you had XDP and DP) you
might want to use (?:^|;)DP=([^;]*) to constrain the field name to begin at the
beginning of the string or after a previous semicolon.
Please let me know if this fixes your SB= problem too. Thanks.
Original comment by rsc@swtch.com
on 29 May 2012 at 2:53
yes, your solution solves both
RE2 pattern("SB=([^;]*)");
gives
the match here has been = -378.92
and
RE2 pattern("AF=([^;]*)");
float match;
gives
the match here has been = 0.02
just to understand the syntax you suggested then
you take zero or more with * of anything except ";" by using the negation [^;]
and that is sufficient to ask for the first match only, because (negation) it
stops as it finds ";".
also, in this syntax you don't need to specify the anycharacter symbol "."
is that correct?
thanks very much for your help, very useful!
Francesco
Original comment by francesc...@gmail.com
on 29 May 2012 at 3:09
Yes, that's exactly right. The full set of supported syntax is described at
code.google.com/p/re2/wiki/Syntax
Original comment by rsc@golang.org
on 30 May 2012 at 12:38
Original issue reported on code.google.com by
francesc...@gmail.com
on 29 May 2012 at 1:43