Dplreplace: Difference between revisions

→‎Examples: trying to make the examples clearer
miraheze:dpl3>Soukupmi
(→‎Examples: breaking example 2 into 2 separate ones)
miraheze:dpl3>Soukupmi
(→‎Examples: trying to make the examples clearer)
Line 19:
{{#dplreplace:abrakadabra|ab|AB}}
</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>
Line 27:
'''Example 2:'''
<pre>
{{#dplreplace:abrakadabra|/a(.*?)r)/|A\1r}}
</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)".
Replaces "a" by "A" if there is an "r" somewhere after the "a". The "\1r" part is needed to put back the original text into the replaced string.<br>
<code><nowiki>{{#dplreplace:abrakadabra|/a(.*?)r/|A}}</nowiki></code> (without the "\1r") would otherwise lead to "{{#dplreplace:abrakadabra|/a(.*?)r/|A}}".
 
'''Result:'''<br>
{{#dplreplace:abrakadabra|/a(.*?)r)/|A\1r}}
 
 
'''Example 3:'''
<pre>
{{#dplreplace:abrakadabra|/a(.*?r)/|A\1}}
</pre>
Replaces "a" bywith "A" if there is an "r" somewhere after the "a". <br>The "\1r1" partreferences isthe neededgroup tofound put backbetween the original"a" textand "r" and puts it back into the replaced string.<br>'''Matches''', with the group match in (): "a(br)", "a(kadabr)".
 
'''Result:'''<br>
{{#dplreplace:abrakadabra|/a(.*?r)/|A\1}}
 
 
'''Example 4:'''
<pre>
{{#dplreplace:abrakadabra|/a(.*?)r/|A\1_r_}}
</pre>
Replaces "a" bywith "A" if there is an "r" somewhere after the "a" and also replaces the "r" at the end of/outside the found group(s) bywith "_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.}}-->