Dplreplace: Difference between revisions

Examples to subpages and linked to and preloaded for basic sandboxing on user subpage.
miraheze:dpl3>Soukupmi
(→‎Examples: trying to make the examples clearer)
imported>FrozenPlum
(Examples to subpages and linked to and preloaded for basic sandboxing on user subpage.)
 
(3 intermediate revisions by the same user not shown)
Line 1:
{{DPL manual|section=dplreplace}}
 
 
This DPL3 module replaces a given <code>''pattern''</code> withing given <code>''text''</code> by the given <code>''replacement''</code>.
 
 
==Syntax==
{{syntax|&#123;&#123;#dplreplace:''text''{{!}}''pattern''{{!}}''replacement''&#125;&#125;}}, where:
<pre>
* <code>''text''</code> is whatever text is to be searched within.
{{#dplreplace:text|pattern|replacement}}
* <code>''pattern''</code> is a regular expression as defined by php <code>preg_replace()</code>.
</pre>
* <code>''replacement''</code> may contain references to matching parts.
 
==Effect==
Replaces the given ''pattern'' within the ''text'' by ''replacement''.
 
'''Note:''' {{regexpinfo}}
''pattern'' is a regular expression as defined by php <code>preg_replace()</code>.
 
''replacement'' may contain references to matching parts.
 
==Examples==
 
'''Example 1:'''
{{#lsth:Examples/Module: dplreplace (example 1)|Example}}
{{ViewTest|Examples|Module: dplreplace (example 1)}}
 
'''Result 1:'''<br>
{{#lsth:Examples/Module: dplreplace (example 1)|Result}}
<pre>
{{#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".
 
'''Result:'''<br>
{{#dplreplace:abrakadabra|ab|AB}}
 
 
'''Example 2:'''
{{#lsth:Examples/Module: dplreplace (example 2)|Example}}
<pre>
{{ViewTest|Examples|Module: dplreplace (example 2)}}
{{#dplreplace:abrakadabra|/a(.*?r)/|A}}
</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)".
 
'''Result 3:'''<br>
{{#lsth:Examples/Module: dplreplace (example 2)|Result}}
{{#dplreplace:abrakadabra|/a(.*?r)/|A}}
 
 
'''Example 3:'''
{{#lsth:Examples/Module: dplreplace (example 3)|Example}}
<pre>
{{ViewTest|Examples|Module: dplreplace (example 3)}}
{{#dplreplace:abrakadabra|/a(.*?r)/|A\1}}
</pre>
Replaces "a" with "A" if there is an "r" somewhere after the "a".<br>The "\1" references the group found between the "a" and "r" and puts it back into the replaced string.<br>'''Matches''', with the group match in (): "a(br)", "a(kadabr)".
 
'''Result 3:'''<br>
{{#lsth:Examples/Module: dplreplace (example 3)|Result}}
{{#dplreplace:abrakadabra|/a(.*?r)/|A\1}}
 
 
'''Example 4:'''
{{#lsth:Examples/Module: dplreplace (example 4)|Example}}
<pre>
{{ViewTest|Examples|Module: dplreplace (example 4)}}
{{#dplreplace:abrakadabra|/a(.*?)r/|A\1_r_}}
</pre>
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.}}-->
 
'''Result 4:'''<br>
{{#lsth:Examples/Module: dplreplace (example 4)|Result}}
{{#dplreplace:abrakadabra|/a(.*?)r/|A\1_r_}}
 
[[Category:Other Modules]]
Anonymous user