regex - Regular expression (.NET): how to get a group in the middle -
i'd group in middle. example:
startgoalend => goal goalend => goal goal => goal
i'm trying (start)?(.*)(end)?
, doesn't lead needed result.
var regex = new regex("(start)?(.*)(end)?"); if (text == null) return; var match = regex.match(text); foreach (var group in match.groups) { console.out.writeline(group); }
it returns:
startgoalend start goalend
how solve regular expressions?
you want avoid capturing start
, end
groups. can avoid capturing contents of pair of parentheses putting ?:
@ start.
you need make capture of middle part lazy, doesn't capture end
part of middle part. can putting ?
after *
.
so altogether, get:
(?:start)?(.*?)(?:end)?$
Comments
Post a Comment