Narendra Kumar
Narendra Kumar

Reputation: 33

Regex expression with placeholders

I am trying to create a regex to find different placeholders in a file. And the placeholders are like
%{hi} and {hi}. If I have n placeholders, want to find all of them. Basic idea is to identify all these placeholders. I am success with individually identifying but not able to combine both to get the desired result.

To find all the matches with %{anything} here are few i tried: %{(.|\\n)\*?} %{.+?} %{\[a-zA-Z1-9\_\]+}

To find all the matches with {anything} here is what I tried: {(\[^}\]+)}** Now I am looking for some help to have these two combined and find all the matches with **%{}** and **{}.** The order can be anyway and these placeholders can be anywhere in the sentence.

Upvotes: 2

Views: 118

Answers (1)

The fourth bird
The fourth bird

Reputation: 163217

You could find both variations using:

%?{[^{}]*}

Explanation

  • %? Match an optional percentage sigh
  • {[^{}]*} Match from {...} using a negated character class matching any character including newlines

Regex demo

Another option matching only word characters in between:

%?{\w+}

Upvotes: 2

Related Questions