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

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -