3

I am using a regex pattern to find instances of [code][/code] BB tags. (This is in PHP with pearl-type regex using preg_match / preg_relace / etc)

'~\[code\](.*?)\[\/code\]~is'

Well, my question is how can I make it so somebody could type something like:

[code][code]code here[/code][/code]

The purpose of typing this would be to demonstrate to a newbie how to place their code into [code][/code] tags.

Currently if I type that, the regex will stop at the 1st instance of "[/code]" and not keep looking ahead to see the 2nd instance of "[code]"

I can't post images since I'm a new user, but here is a screenshot of the output: https://i.stack.imgur.com/7gS6x.png

I know there is a term in regex called "positive look ahead" and "negative look ahead", but I'm not quite sure what they mean, or if they are relevant to my situation. Could someone please give me a hand? Thank you.

EDIT: I'm sorry but I don't seem to have enough rep to +1 anything. I really appreciate your help, and it was so fast.

Lakey
  • 1,948
  • 2
  • 17
  • 28
  • Thanks for that link. I didn't know that was a button. I was originally going to accept WordsWorth's answer because it works great, but I decided to accepted your answer instead because it's more flexible and won't fail to match if someone types other things in the code tags. Thanks! – Lakey Feb 14 '12 at 05:29

3 Answers3

2

you can try for this pattern

'~\[code\](.*?)(\[\/code\])+~is'
WordsWorth
  • 872
  • 1
  • 11
  • 23
1

If you want to cover also the case that there can be more code between the two closing tags you can try this one

\[code\].*?\[\/code\](?:(?:(?!\[code\]).)*\[\/code\])?

See it here on Regexr

The first part \[code\].*?\[\/code\] is matching from the first opening tag to the first closing tag.

Then comes the tricky part that is now optional. (?:(?:(?!\[code\]).)*\[\/code\])? is matching characters, if the there is not an opening tag following, till the last closing tag found.

stema
  • 90,351
  • 20
  • 107
  • 135
  • stema, thanks for your reply. While this pattern does a great job matching something like [code]asdf[code]asdf[/code]asdf[/code] it unfortunately can't match the "normal" code anymore such as [code]asdf[/code] Thanks! – Lakey Feb 12 '12 at 22:38
  • @Lakey I updated my answer and made the lookahead part optional. – stema Feb 13 '12 at 06:52
0

All you are looking at is removing the ? in the (.*?)

With the ?, it is a lazy match and will match few characters as possible and that is why it stops when it sees the first [/code]

Without the ?, it is a greedy match and will match as many characters as possible and will match till the outer end tag.

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • manojlds, Thank you. However, while that certainly did work for the nested [code] tags, it had an undesirable side-effect. Now, non-nested [code] tags are all lumped together. Here is a "before "screenshot: http://imgur.com/18bAD And here is an "after" screenshot: http://imgur.com/Vkys8 Is there a way to have the best of both worlds? – Lakey Feb 10 '12 at 04:40