Capture text between two known characters

Asked

Viewed 1,532 times

3

I need to pick up any text and find the words (or phrases) that are between two specific characters { and }, in that case.

I can do this by capturing the delimiters together, using the expression {(.*?)}, but I need the string come without the two limit characters ({}).

I tagged but I accept answers that do not use regex.

  • 1

    Try this: [^{(.*?)}]

2 answers

3


Solution in Regex:

  • With {}

    Regex r = new Regex(@"\{[^\}]+?\}");
    Match m = r.Match(text);
    
  • Without {}:

    Regex r = new Regex(@"(?<=\{)[^\}]+?(?=\})");
    Match m = r.Match(text);
    

See on .netFiddle

  • I forgot to tell you I need it without the keys...

1

The expression:

([a-z0-9])+(?=\})

Browser other questions tagged

You are not signed in. Login or sign up in order to post.