Dplreplace: Difference between revisions
miraheze:dpl3>Soukupmi (→Examples: breaking example 2 into 2 separate ones) |
miraheze:dpl3>Soukupmi (→Examples: trying to make the examples clearer) |
||
Line 19: | Line 19: | ||
{{#dplreplace:abrakadabra|ab|AB}} |
{{#dplreplace:abrakadabra|ab|AB}} |
||
</pre> |
</pre> |
||
Replaces "ab" with "AB".<br>This is the basic usage without using regular expressions as pattern.<br>'''Matches''': "ab", "ab". |
|||
Replaces "ab" with "AB". |
|||
'''Result:'''<br> |
'''Result:'''<br> |
||
Line 27: | Line 27: | ||
'''Example 2:''' |
'''Example 2:''' |
||
<pre> |
<pre> |
||
{{#dplreplace:abrakadabra|/a(.*? |
{{#dplreplace:abrakadabra|/a(.*?r)/|A}} |
||
</pre> |
</pre> |
||
Replaces "a" ''and the following characters until the next "r"'' with "A".<br>This example uses a regular expression as pattern, indicated by the "/", and is using the lookahead syntax "?" to find the "r".<br>'''Matches''', with the group match in (): "a(br)", "a(kadabr)". |
|||
⚫ | |||
<code><nowiki>{{#dplreplace:abrakadabra|/a(.*?)r/|A}}</nowiki></code> (without the "\1r") would otherwise lead to "{{#dplreplace:abrakadabra|/a(.*?)r/|A}}". |
|||
'''Result:'''<br> |
'''Result:'''<br> |
||
{{#dplreplace:abrakadabra|/a(.*? |
{{#dplreplace:abrakadabra|/a(.*?r)/|A}} |
||
'''Example 3:''' |
'''Example 3:''' |
||
<pre> |
|||
{{#dplreplace:abrakadabra|/a(.*?r)/|A\1}} |
|||
</pre> |
|||
⚫ | |||
'''Result:'''<br> |
|||
{{#dplreplace:abrakadabra|/a(.*?r)/|A\1}} |
|||
'''Example 4:''' |
|||
<pre> |
<pre> |
||
{{#dplreplace:abrakadabra|/a(.*?)r/|A\1_r_}} |
{{#dplreplace:abrakadabra|/a(.*?)r/|A\1_r_}} |
||
</pre> |
</pre> |
||
Replaces "a" |
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_".<br>'''Matches''', with the group match in (): "a(b)r", "a(kadab)r". |
||
<!--{{note|This explanation is incomplete, it needs to be expanded to better explain what is taking place in the result.}}--> |
<!--{{note|This explanation is incomplete, it needs to be expanded to better explain what is taking place in the result.}}--> |
||
Revision as of 12:27, 5 April 2022
|
Manual | dplreplace |
Syntax
{{#dplreplace:text|pattern|replacement}}
Effect
Replaces the given pattern within the text by replacement.
pattern is a regular expression as defined by php preg_replace()
.
replacement may contain references to matching parts.
Examples
Example 1:
{{#dplreplace:abrakadabra|ab|AB}}
Replaces "ab" with "AB".
This is the basic usage without using regular expressions as pattern.
Matches: "ab", "ab".
Result:
{{#dplreplace:abrakadabra|ab|AB}}
Example 2:
{{#dplreplace:abrakadabra|/a(.*?r)/|A}}
Replaces "a" and the following characters until the next "r" with "A".
This example uses a regular expression as pattern, indicated by the "/", and is using the lookahead syntax "?" to find the "r".
Matches, with the group match in (): "a(br)", "a(kadabr)".
Result:
{{#dplreplace:abrakadabra|/a(.*?r)/|A}}
Example 3:
{{#dplreplace:abrakadabra|/a(.*?r)/|A\1}}
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)".
Result:
{{#dplreplace:abrakadabra|/a(.*?r)/|A\1}}
Example 4:
{{#dplreplace:abrakadabra|/a(.*?)r/|A\1_r_}}
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".
Result:
{{#dplreplace:abrakadabra|/a(.*?)r/|A\1_r_}}