简体   繁体   中英

Regular expression to match square bracket url tags

I'm trying to create a regular expression that will match square bracket url tags as follows:-

[url]some text[/url]

or

[url class="class"]some text[\url]

This is the pattern I have created

(\[url.*\])(.*?)(\[\\url\])

It works fine if there is only one tag however if I have two tags in a sentence as follows:

This is a sentence [url]blah[\url] this is another sentence[url]blah[\url]

It only has one match and grabs everything between the first opening and last closing [url] tag. I did some research and added the ? to stop it being greedy and grabbing everything but it doesn't work. I also tried using:

[^\[]* 

instead of

(.*?)

again it doesn't make a difference.

It's the first .* in your regex that's causing it not to work properly. Try this:

(\[url[^\]]*\])([^\[]*)(\[\\url\])

.* is being greedy and matches everything. If you check this group from your current regex, you'll actually see [url]blah[\\url] this is another sentence[url] as the match, blah in the second group and [\\url]\u003c/code> in the third group.

.* can be a bit dangerous, if you know in advance you are goig to have text/numbers you coud use [a-zA-Z0-9] and avoid that problem.

Something like: \\[url\\][a-zA-Z0-9]+\\[\\\\url\\]

更具体地说,你可以给:

(\[url( .+[=].+\])*)[z-zA-Z0-9 ](\[\url\]) 

For url tags only:

(?:\[url])([^\[]+)\[[\/\\]url]

Live demo

To remove all tags:

(?:\[(\w+)[^]]*]).*?\[[\/\\]\1]

Live demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM