1

I've been working on this for quite some time, but I would need to learn the regex first, and I will do it next. This can be a good example for other people, so how could something like this be explained?

(?:  [^[]   |   \[  (?! /?indent]   )   |   (?R)    )

(?: ...) - this is a non-capturing group, the whole thing here

[^[] - match anything that is not the "["

| - OR ("22|33"; this will match either "22" or "23")

(?! ...) - negative lookahead. Specifies a group that cannot match after the main expression (if it matches the result is discarded); (?!theatre)the\w+ - matches “theme” but not “theater”

/? - /? – once or none; plurals?, matches plural, plurals

I was reading about recursion, but I still dont understand this part in this context.

I guess we are looking for "[indent]" and "[/indent]" overall, but with all the above context that I dont fully get. This is taken from an example at php.net:

http://php.net/manual/pl/function.preg-replace-callback.php Example #3

Thanks.

  • \[indent]((?:[^[]|\[(?!/?indent])|(?R))+)\[/indent] - this is the whole thing, and this is a patter to search for with preg_replace_callback() –  Aug 18 '17 at 07:20
  • And [here is the whole explanation with the demo](https://regex101.com/r/QZQFxN/1). – Wiktor Stribiżew Aug 18 '17 at 07:28
  • Thanks! I was getting some errors with these tools. I was working on learning Lambdas and Closures and I got to that in one of the examples related to that. I've been working on this for quite some time and I am kind of stuck. I would like to finish what I was doing and start working on learning the Regex next. Any good tools / sites for that? –  Aug 18 '17 at 07:32
  • I see. The point here is that the pattern should match only the `[` that is not followed with `/indent]` or `indent]` (see `(?!/?indent])`), and the whole pattern is recursed with `(?R)` (not just the group). – Wiktor Stribiżew Aug 18 '17 at 07:36
  • I can suggest doing all lessons at [regexone.com](http://regexone.com/), reading through [regular-expressions.info](http://www.regular-expressions.info), [regex SO tag description](http://stackoverflow.com/tags/regex/info) (with many other links to great online resources), and the community SO post called [What does the regex mean](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean). Also, [rexegg.com](http://rexegg.com) is worth having a look at. A [great book on regex](http://files.hii-tech.com/book/Oreilly.Mastering.Regular.Expressions.3rd.Edition.Aug.2006.pdf). – Wiktor Stribiżew Aug 18 '17 at 07:38
  • Thanks again. I will be doing all that. I am learning PHP and as an example I am reading the whole manual at php.net –  Aug 18 '17 at 07:40
  • Ok, so basically, regex-wise, the point here is to have equal number of "[indent]" and "[/indent]" in the mix? So something like this "deep [indent] deeper [/indent] deep" matches and something like this would not - "deep [indent] deeper [/indent] even-deeper [/indent] deep"? So basically we want to have matching number of opening and closing tags? –  Aug 18 '17 at 08:14
  • The point is to match `A...B` or `A..A..B...B`. So, in `deep [indent] deeper [/indent] even-deeper [/indent] deep`, the `[indent] deeper [/indent]` will match. – Wiktor Stribiżew Aug 18 '17 at 09:17

0 Answers0