Что-то вроде этого будет работать для тегов, у которых нет атрибутов:
\[(b|i|u)(:[a-z0-9]+)?\](.*?)\[\/\1(?:\2)?\]
\[ -- matches literal "["
(b|i|u) -- matches b, i, or u, captures as backreference 1
(:[a-z0-9]+)? -- matches colon and then alphanumeric string, captures as backreference 2
-- the question mark allows the :string not to be present.
\] -- matches literal "]"
(.*?) -- matches anything*, as few times as required to finish the match, creates backreference 3.
\[ -- matches literal "["
\/ -- matches literal "/"
\1 -- invokes backreference 1 to make sure the opening/closing tags match
(?:\2)? -- invokes backreference 2 to further make sure it's the same tag
\] -- matches literal "]"
Сопоставить тег, такой как URL, достаточно просто
С тегами, имеющими атрибуты, они делают разные вещи со своими атрибутами, поэтому, вероятно, проще обрабатывать тег, такой как URL, отдельно от тега, такого как IMG.
\[(url)(?:\s*=\s*(.*?))?(:[a-z0-9]+)\](.*?)\[\/\1(?:\3)?\]
\[ -- matches literal "["
(url) -- matches literal "url", in parentheses so we can invoke backreference 1 later, easier for you to modify
(?: -- ?: signifies a non-capturing group, so it creates a group without creating a backreference, or altering the backreference count.
\s*=\s* -- matches literal "=", padded by any amount of whitespace on either side
(.*?) -- matches any character, as few times as possible, to complete the match, creates backreference 2
) -- closes the noncapturing group
(:[a-z0-9]+) -- matches the alphanumeric string as backreference 3.
\] -- matches literal "]"
(.*?) -- matches any character as few times as possible to complete the match, backreference 4
\[ -- matches literal "["
\/ -- matches literal "/"
\1 -- invokes backreference 1
(?:\3)? -- invokes backreference 3
\] -- matches literal "["
Для вашей замены содержимое тегов находится в самих обратных ссылках, поэтому вы можете сделать что-то подобное для тегов b/i/u.
<\1>\3</\1>
Для тега URL это примерно так
<A href="\2">\4</A>
Я говорю, что точка/точка соответствует любому символу в нескольких местах. Соответствует любому символу, кроме новой строки. Вы можете включить модификатор новой строки в своем регулярном выражении, используя модификатор "dotall"
s
, как это
/(.*)<foo>/s
person
Regular Jo
schedule
13.12.2014