Home > other >  What is the best way to tag these strings using Regex Expressions?
What is the best way to tag these strings using Regex Expressions?

Time:01-23

#{config_name=Scene&sn={2}&field=name}

#{pos_x={3}&y={4}&z={5}&stage={6}&stageId={7}&content=Go Now&http=true&underline=true}

{{rescue_{1}_{2}_Rescue Now}}

# \{.*?\}

I tried to use this expression but it didn't help

CodePudding user response:

My solution that will match either strings with '#' at start and multiple params inside the brackets, either anything inside double brackets (if this is what you want)

(#\{[a-zA-Z_] =.*(&[a-zA-Z_] =.*)*\})|(\{\{.*\}\})

CodePudding user response:

You could either repeat matching the key=value pairs with an ampersand in between for the #{....} strings, or for the {{...}} strings match the curly's at the start and end where you would only match curly's in between that contain digits.

^(?:#{[^=&] =[^=&] (?:&[^=&\n] =[^=&\n] )*}|{{[^{}]*(?:{\d }[^{}]*)*}})$

See a regex101 demo.

Another bit broader match is to match the same key=value pairs, or match {{...}} from the start till the end of the string allowing any character in between:

^(?:{{.*}}|#{[^{}\n]*(?:{\d }[^{}\n]*)*})

See another regex101 demo.

  • Related