0

(.[^\n]*?)\[code](.*?)\[\/code](.[^\n]*?)(*SKIP)(*F)|(.[^\n]*?)\[php](.*?)\[\/php](.[^\n]*?)(*SKIP)(*F)|some_rules_here

The pattern above makes the function work too slow.

With it: http://regex101.com/r/qP4tT5/1 Page gets loaded in more than 3 seconds. Regex debugger says it took 5153 steps in total to execute the regex.

Without it: http://regex101.com/r/fG4tW0/1 Page gets loaded in only 0,3 seconds Regex debugger says it took only 4 steps to execute the regex.

Why do I have to use it: Because if the emoticon's shortcut is inside a [code] or [php] tag, it shouldn't be HTMLed.

Is there an alternative method which does the same thing faster?

Wellenbrecher
  • 179
  • 1
  • 9
  • 1
    You should specify what the "thing" you want to do is exactly. With sample input and output. – jeroen Aug 13 '14 at 22:07
  • `[code]` and `[php]`? Emoticons? Are you parsing BBCode from forum posts? BBCode is an irregular language (like HTML), and we all know what happens when you [try to parse HTML with regex](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags). I think you need a BBCode parser (which can be blindingly fast). – Darth Android Aug 13 '14 at 22:08
  • @DarthAndroid @jeroen My question is clear enough. I have a BBCode and Emoticon parser(made using preg_replace and preg_match functions) which is developed by myself. It works perfectly without any errors. The only problem is its speed. If a BBCode or an emoticon is inside a `[code]` or `[php]` tag, the parser doesn't replace it and skips it as what it should do. But like I said before, it is too slow. – Wellenbrecher Aug 13 '14 at 22:16
  • `[code]:([code]:([/code]:([/code]` Should all of these smilies be skipped? – Darth Android Aug 13 '14 at 22:21
  • @Wellenbrecher Then you cannot use regular expressions to build a pseudoparser that achieves that for arbitrary input. You need a proper BBCode parser. – Darth Android Aug 13 '14 at 22:35

1 Answers1

1

You can simplify this regular expression a bit.

~\[(code|php)][^[]*\[/\1](*SKIP)(*F)|:\(~i

enter image description here

Live Demo

hwnd
  • 69,796
  • 4
  • 95
  • 132
  • This is just what I need! Thank you. But it didn't work well in php. I think the `[/\1]` part causes the problem. The \ character escapes `1` and gives error. And when I escape the slash like `[/\\1]`, it says "unknown modifier '\' ". How can I fix this? – Wellenbrecher Aug 13 '14 at 22:34
  • @Wellenbrecher Use `$1` instead then. – Darth Android Aug 13 '14 at 22:37
  • No it does not handle nesting. OPs original regex does not either. – hwnd Aug 13 '14 at 22:41
  • 1
    Sorry, my fault. I didn't see you used ~ instead of /. My parser is working faster now. The page gets loaded only in 0,1 seconds. It was taking 0,3 seconds before. Thank you again! – Wellenbrecher Aug 13 '14 at 22:43