ViqMontana
ViqMontana

Reputation: 5688

How can I write regex for the following statements?

I have some strings, and I would like to use regex to extract any html tags, and also the text from a set of curly braces.

For example, I can have the following 2 strings:

Is this a { <strong>  Versioned placeholder </strong> } file?
Is this a  <strong> { Versioned placeholder } </strong> file?

And so far, I have the following regex:

(?:\{)(?<PlaceholderValue>\s*[\w\s]*\s*)(?:\})

What I would like is for the text within the curly braces (i.e. "Versioned placeholder") to be placed in the PlaceholderValue group, and all other html tags WITHIN the curly braces to also be captured. How can I do this?

Note, the html tags within the curly braces are optional, and will not always be present. I am not interested if the html tags are outside the curly braces.

Upvotes: 1

Views: 51

Answers (3)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

You may use

(?:{|(?!^)\G)\s*\K(?:(?<tag><[^>]+>)|(?<PlaceholderValue>[^<}]*[^<}\s]))

See the regex demo

The pattern matches:

  • (?:{|(?!^)\G) - a { or the end of the previous successful match
  • \s* - 0+ whitespaces (to trim from the left)
  • \K - match reset operator
  • (?:(?<tag><[^>]+>)|(?<PlaceholderValue>[^<}]*[^<}\s])) - A group of 2 alternatives:
    • (?<tag><[^>]+>) - Group "tag" matching a <, 1+ chars other than < and >, and a >
    • | - or
    • (?<PlaceholderValue>[^<}]*[^<}\s]) - Group "PlaceholderValue" capturing 0+ chars other than < and } as many as possible, and then an obligatory character that is not a whitespace, < and }.

Upvotes: 2

T&#226;n
T&#226;n

Reputation: 1

You can try this in javascript:

var string1 = 'Is this a { <strong>  Versioned placeholder </strong> } file?';
var string2 = 'Is this a  <strong> { Versioned placeholder } </strong> file?';

var reg = /<(strong)>[\{\}\w\s]+<\/\1>/;

alert(string1.match(reg)[0].replace(/<strong>|<\/strong>|{|}/g, ''));
alert(string2.match(reg)[0].replace(/<strong>|<\/strong>|{|}/g, ''));

Upvotes: 0

JDro04
JDro04

Reputation: 670

(?<=\{ )(.*?)(?= \})

This should work depending on what regex you're using

Upvotes: 0

Related Questions