c# - Match regex between curly braces -


i need match regular expression tokens text:

hello {fullname}, wanted inform product {productname} ready. please come our address {address} it! 

how can match specific tokens in text , fill values using regex?

also need in safe way , avoid every possible issues tokens misspelled or have wrong, bellow:

**hello {full{name}, { wanted inform product {{productname} ready. please come our } address {addr{street}ess} it!** 

p.s.

i tried this: {([^}]+)}

but if have example :

 {fullname}    

it works, work if have

{full{name} ... 

p.s. 2:

i tried this: {=[^{^=^}]*=} have use character instead of curly braces ... possible adjust work without equal character?

 {=fullname=}    - works  {=full{name=}   - doesn't work 

so token between {=token=} instead of {token}

this might give starting point. handle exceptions you'd like.

the method travels through input string, setting 'open' flag , index when finds opentoken. when 'open' flag true, , closetoken found, extracts substring based on index , current position.

if throwonerror property set true, , token found in unexpected location, throw exception.

this code modified handle unexpected tokens differently... such skipping match entirely, adding match as-is, or whatever desire.

public class customtokenparser {     public char opentoken { get; set; }     public char closetoken { get; set; }      public bool throwonerror { get; set; }      public customtokenparser()     {         opentoken = '{';         closetoken = '}';         throwonerror = true;     }      public customtokenparser(char opentoken, char closetoken, bool throwonerror)     {         this.opentoken = opentoken;         this.closetoken = closetoken;         this.throwonerror = throwonerror;     }              public string[] parse(string input)     {         bool open = false;         int openindex = -1;         list<string> matches = new list<string>();          (int = 0; < input.length; i++)         {             if (!open && input[i] == opentoken)             {                 open = true;                 openindex = i;             }             else if (open && input[i] == closetoken)             {                 open = false;                 string match = input.substring(openindex + 1, - openindex - 1);                 matches.add(match);             }             else if (open && input[i] == opentoken && throwonerror)                 throw new exception("open token found while match open");             else if (!open && input[i] == closetoken && throwonerror)                 throw new exception("close token found while match not open");         }          return matches.toarray();     } } 

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -