c# - Regex return between -
i have following code. note wild card want. (please forgive bad code before final refactor).
string findregex = "{f:.*}"; regex regex = new regex(findregex); foreach (match match in regex.matches(blockoftext)) { string findcapture = match.captures[0].value; string between = findcapture.replace("{f:", "").replace("}", ""); }
what dont code trying in between found, double replace statements. there better way?
additional: here sample string
dear {f:firstname} {f:lastname},
you can use parens group part of match , extract later:
string findregex = "{f:(.*?)}"; regex regex = new regex(findregex); foreach (match match in regex.matches(blockoftext)) { string between = match.captures[1].value; }
if want 2 matches (e.g. first-name last-name example), make 2 groups:
string findregex = "{f:(.*?)}.*?{f:(.*?)}"; regex regex = new regex(findregex); foreach (match match in regex.matches(blockoftext)) { string firstname = match.captures[1].value; string lastname = match.captures[2].value; }
Comments
Post a Comment