Dplreplace: Difference between revisions

From DynamicPageList3 Manual
Content added Content deleted
imported>FrozenPlum
mNo edit summary
imported>FrozenPlum
(Examples to subpages and linked to and preloaded for basic sandboxing on user subpage.)
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{purgenote}}
{{DPL manual|section=dplreplace}}
{{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==
{{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==
==Examples==


'''Example 1:'''
'''Example 1:'''
{{#lsth:Examples/Module: dplreplace (example 1)|Example}}
{{ViewTest|Examples|Module: dplreplace (example 1)}}

'''Result 1:'''
{{#lsth:Examples/Module: dplreplace (example 1)|Result}}
<pre>
<pre>
{{#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".

'''Result:'''<br>
{{#dplreplace:abrakadabra|ab|AB}}




'''Example 2:'''
'''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:'''<br>
'''Result 3:'''<br>
{{#lsth:Examples/Module: dplreplace (example 2)|Result}}
{{#dplreplace:abrakadabra|/a(.*?r)/|A}}




'''Example 3:'''
'''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:'''<br>
'''Result 3:'''<br>
{{#lsth:Examples/Module: dplreplace (example 3)|Result}}
{{#dplreplace:abrakadabra|/a(.*?r)/|A\1}}




'''Example 4:'''
'''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:'''<br>
'''Result 4:'''<br>
{{#lsth:Examples/Module: dplreplace (example 4)|Result}}
{{#dplreplace:abrakadabra|/a(.*?)r/|A\1_r_}}


[[Category:Other Modules]]
[[Category:Other Modules]]

Latest revision as of 02:57, 11 April 2023

Manual dplreplace


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


Syntax

{{#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

Example 1: {{#lsth:Examples/Module: dplreplace (example 1)|Example}}

Page Template:ViewTest/style.css must have content model "Sanitized CSS" for TemplateStyles (current model is "plain text").

Result 1: {{#lsth:Examples/Module: dplreplace (example 1)|Result}}

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


Example 2: {{#lsth:Examples/Module: dplreplace (example 2)|Example}}

Page Template:ViewTest/style.css must have content model "Sanitized CSS" for TemplateStyles (current model is "plain text").

Result 3:
{{#lsth:Examples/Module: dplreplace (example 2)|Result}}


Example 3: {{#lsth:Examples/Module: dplreplace (example 3)|Example}}

Page Template:ViewTest/style.css must have content model "Sanitized CSS" for TemplateStyles (current model is "plain text").

Result 3:
{{#lsth:Examples/Module: dplreplace (example 3)|Result}}


Example 4: {{#lsth:Examples/Module: dplreplace (example 4)|Example}}

Page Template:ViewTest/style.css must have content model "Sanitized CSS" for TemplateStyles (current model is "plain text").

Result 4:
{{#lsth:Examples/Module: dplreplace (example 4)|Result}}