How to make an optional plot of regular expression in preg_replace_callback?

There are 2 conditions preg_replace_callback:
if (strpos (self::$template, "[authis]") !== false) {
 self::$template = preg_replace_callback("#\\[authis\\](.*?)\\[else\\](.*?)\\[/authis\\]#is", function($matches){
 return $matches[1];
 }, self::$template);
}

 if (strpos (self::$template, "[authis]") !== false) {
 self::$template = preg_replace_callback("#\\[authis\\](.*?)\\[/authis\\]#is", function($matches){
 return $matches[1];
 }, self::$template);
 }


These 2 conditions work well, but I would like it to combine them, if possible. The first regular season looking for a design as "[authis]Text[else]Text[/authis]", and the second is looking for [authis]Text[/authis].

The next question. How to make to find both designs at once. That is, in the first designs should be ignored plot "\\[else\\](.*?)", if it is not in the structure itself.

If there is no "[else]Text" that ignored "\\[else\\](.*?)".
July 2nd 19 at 14:13
2 answers
July 2nd 19 at 14:15
Solution
Ignorance ?
Why not make
#\\[authis\\](.*?)(\\[else\\](.*?))?\\[/authis\\]#is

But note that I added parentheses so that $matches[...] should be reviewed.

UPD https://regex101.com/r/hd6CCL/1
Thanks for the speedy response. Right now check. - ismael97 commented on July 2nd 19 at 14:18
"Ignore", well, that part [else] would like to exist and not be present. Just if my first condition is not to put [else], then it won't work. - ismael97 commented on July 2nd 19 at 14:21
: I meant that ignoring is indicated in regexps as a question mark. - aniya.Wintheis commented on July 2nd 19 at 14:24
: (\\[else\\](.*?)) Perceives as a string. $matches[2][0] outputs " [ " , and so on. - ismael97 commented on July 2nd 19 at 14:27
Clear. We had $matches[2], process explode, well, one word to remove in the beginning [else] from the string. Thank you. - ismael97 commented on July 2nd 19 at 14:30
: Also works $matches[3] - aniya.Wintheis commented on July 2nd 19 at 14:33
July 2nd 19 at 14:17
(plot)*
Said above. Not * and ?. * denotes any symbol. - ismael97 commented on July 2nd 19 at 14:20
: * - not any character, and the repetition of the preceding part 0 or more times. And any symbol . (point) - ismael97 commented on July 2nd 19 at 14:23

Find more questions by tags PHPRegular expressions