Jump to content

Dplreplace

From DynamicPageList3 Manual
Manual dplreplace


This DPL3 module replaces a given pattern withing given text by the given replacement.


Syntax[edit]

{{#dplreplace:text|pattern|replacement}}, where:

  • text is whatever text is to be searched within.
  • pattern is a regular expression as defined by php preg_replace().
  • replacement may contain references to matching parts.


Note: If you are not familiar with regular expressions and/or do not know the specifics of Perl regexp used in PHP, it may be helpful to see:

Examples[edit]

Example 1: This replaces "ab" with "AB". This is the basic usage without using regular expressions as pattern.

{{#dplreplace:abrakadabra|ab|AB}}

Result 1: ABrakadABra

{{#dplreplace:abrakadabra|ab|AB}}


Example 2: This replaces "a" and its following characters until the next "r", with "A". This example uses a regular expression as pattern, indicated by the "/" characters, and is using the lookahead syntax ? to find the "r".

Matches, with the group match in (): "a(br)", "a(kadabr)".

{{#dplreplace:abrakadabra|/a(.*?r)/|A}}

Result 3:
AAa


Example 3: Replaces "a" with "A" if there is an "r" somewhere after the "a". The \1 references the group found between the "a" and "r" and puts it back into the replaced string.

Matches, with the group match in (): "a(br)", "a(kadabr)".

{{#dplreplace:abrakadabra|/a(.*?r)/|A\1}}

Result 3:
AbrAkadabra


Example 4: Replaces "a" with "A" if there is an "r" somewhere after the "a", and also replaces the "r" at the end of/outside the found group(s) with "_r_".

Matches, with the group match in (): "a(b)r", "a(kadab)r".

{{#dplreplace:abrakadabra|/a(.*?)r/|A\1_r_}}

Result 4:
Ab_r_Akadab_r_a

Cookies help us deliver our services. By using our services, you agree to our use of cookies.