Regex: how to get all the contents inside the tag # [SOME TEXT HERE]

I am working on a simple token replacement function for our product. I almost solved the whole problem, but I missed one thing. A token must support attributes, and an attribute can also be a token. This is part of a larger project. hope you can help.

The start tag is equal "**#[**", and the end tag "**]**". Let's say # [FirstName], # [LastName], # [Age, WhenZero = "Undisclosed"].

I am using this expression now "\#\[[^\]]+\]". It works for me, but it failed on this input:

blah blah text here...
**#[IsFreeShipping, WhenTrue="<img src='/images/fw_freeshipping.gif'/>
<a href='http://www.hellowebsite.net/freeshipping.aspx'>$[FreeShipping]</a>"]**
blah blah text here also...

He fails because he appears first], he stops there. It returns:

*#[IsFreeShipping, WhenTrue="<img src='/images/fw_freeshipping.gif'/>
<a href='http://www.hellowebsite.net/freeshipping.aspx'>$[Product_FreeShipping]*

My desired result should be

*#[IsFreeShipping, WhenTrue="<img src='/images/fw_freeshipping.gif'/>
<a href='http://www.hellowebsite.net/freeshipping.aspx'>$[FreeShipping]</a>"]*
+3
source share
7

, , ...

#\[(\](?=")|[^\]])+\]

.

, , , .

- , ...


lookahead , .
, , , lookahead:

#\[(\](?=</a>")|[^\]])+\]

, </a>".

, (.*]) , , . (, ], .)

+1

: first.

, , . , , , .

+1
0

, :

/#\[.*\]/

, , . - .

(Regex - - , - )

0

, , , , . Dyck, pushdown . , , , Perl.

0

, , . .

, DSL "if". .

, Django:

:

blah blah text here... #[IsFreeShipping, 
WhenTrue="<img src='/images/fw_freeshipping.gif'/>
<a href='http://www.hellowebsite.net/freeshipping.aspx'>$[FreeShipping]</a>"]
blah blah text here also...

Django:

blah blah text here... {% if IsFreeShipping %}
<img src='/images/fw_freeshipping.gif'/>
<a href='http://www.hellowebsite.net/freeshipping.aspx'>{{ FreeShipping }}</a>
{% endif %} blah blah text here also...
0

:

#\[(?:[^\]$]+|\$(?!\[)|\$\[[^\[\]]*\])*\]

, . , , , . , regex.: D

Tis regex also refers to "$" as special only if followed by a square opening bracket. If you want to prohibit its use otherwise, remove the second alternative:|\$(?!\[)

0
source

Source: https://habr.com/ru/post/1702567/


All Articles