Difference between revisions of "ReNamer:Rules:Replace"

From den4b Wiki
Jump to navigation Jump to search
m (general cleanup, answered the question)
(→‎Wildcards: Backreferences explained)
 
(30 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 +
{{Go|up=ReNamer:Rules|prev=ReNamer:Rules:Remove|next=ReNamer:Rules:Rearrange}}
 +
 
== Replace Rule ==
 
== Replace Rule ==
[[Image:]]This rule removes the specified string from the name and replaces it with another string. It has options to replace the first occurrence only, the last occurrence only, or all the occurrences. You can replace multiple strings at a time. The rule will replace ''each'' of them with the same replacement string. You can create a pattern with wild cards, so that any string that matches the pattern will be removed.
 
  
The parameters are as follows:
+
[[Image:ReplaceRule.png|center]]
  
{| class="prettytable"
+
This rule removes the specified string from the name and replaces it with another string. It has options to replace the first occurrence, the last occurrence, or all the occurrences. You can replace multiple strings at a time. You can also use wildcards to create simple patterns.
! Parameter
+
 
 +
The parameters are as follows:
 +
 
 +
{| class="wikitable"
 +
|-
 +
! Parameter  
 
! Details
 
! Details
 
 
|-
 
|-
| Find
+
| Find  
| Enter the string to be replaced.
+
| Enter the string to be replaced.  
 
+
*You can enter multiple strings at a time. They will be searched & replaced in the order as they appear. Press the + button to insert a separator between two strings. Instead of pressing this button, you can also enter '''*|*''' from the keyboard.
* You can enter multiple strings at a time. Press the + button to separate two strings
+
*If the name does not contain the specified string, the rule will not act on it.  
* If the name does not contain the specified string, the rule will not act on it.
+
*If the name contains more than one of these strings, it will replace them according to the Occurrences parameter.
* If the name contains more than one of these strings, it will replace all of them.  
 
  
 
|-
 
|-
| [[Image:]] button
+
| [[Image:PlusButton.png]] button  
| Press to insert a <nowiki>*|*</nowiki> separator between strings.
+
| Inserts a separator ('''*&#124;*''') sequence between two delimiter entries.
 
 
'''@@@ actually this inserts a separator in Replace box also. What does this mean? That pairs are replaced? (e.g. A →A', B->B' etc.)'''
 
  
<span style="color: blue; font-weight: bold">@@@ YES, so multiple related replace operations can be combined into one rule.</span>
+
In that case, the n<sup>th</sup> entry in the '''Find''' field is replaced by the n<sup>th</sup> entry in the '''Replace''' field.
  
 
|-
 
|-
| Replace
+
| Replace  
| Enter the string that will replace the "Find" string.
+
| Enter strings that will replace the "Find" strings.  
  
* Note that although the rule can find different strings, it can use only one string as replacement. If you do not want that, you will have to add the '''Replace''' rule multiple times, with specific pairs of ''find ''and ''replace'' strings.
+
Note that the number of strings (separated with '''*&#124;*''') in "Find" and "Replace" fields should be the same. If there is more strings in the "Find" field than in the "Replace" field the spare strings will be removed (replaced with an empty string). If there is more strings in the "Replace" field, the spare strings will be ignored.
  
 
|-
 
|-
| Insert meta tag [[Image:]]
+
| Insert meta tag  
| Click the button to see a list of meta-tags.
+
[[Image:ReNamer Insert Meta Tag Button.png]]  
  
 +
| Click the button to see a list of [[ReNamer:Meta Tags|meta-tags]].
 
|-
 
|-
| Occurrences
+
| Occurrences  
| In case the strings occurs multiple times in the name, specify which occurrences should be replaced. (Options are- ''First only'', ''last only'', or ''all'')
+
| In case that strings occur more than once in the filename, specify which occurrences should be replaced.
 
 
 
|-
 
|-
| Skip extension
+
| Skip extension  
| If this check box is unselected, the extension will be included in the rule.
+
| If checked, the file extension will be excluded from the processing and will remain unaffected.
 
 
 
|-
 
|-
| Case sensitive
+
| Case sensitive  
 
| Will only remove a specified string from the name if the case matches exactly.
 
| Will only remove a specified string from the name if the case matches exactly.
 +
|-
 +
| Whole words only
 +
| Remove the subject text only when it is whole word, not a part of another word.
  
 +
For example, searching for "bar" would not find a match in "foobar" or "bars".
 
|-
 
|-
| Interpret symbols as wild cards
+
| Interpret symbols as wild cards  
| The following wild cards are allowed (compare with RegEx).
+
| Treat certain symbols as [[#Wildcards|Wildcards]] for matching simple patterns (similar to [[ReNamer:Regular Expressions|Regular Expressions]]).  
  
{| class="prettytable"
+
|}
!
+
 
 +
=== Wildcards ===
 +
 
 +
{| class="wikitable"
 +
|-
 +
! Wildcard
 
! Represents
 
! Represents
 
! Example
 
! Example
 
 
|-
 
|-
| <center><nowiki>*</nowiki></center>
+
| <center>*</center>  
| any number of characters (including numbers, space, underscores, etc.).
+
| any number of characters (including numbers, space, underscores, etc.).  
 
| '''abc*''' equals '''abc''' followed by 0 or more characters.
 
| '''abc*''' equals '''abc''' followed by 0 or more characters.
 +
|-
 +
| <center>?</center>
 +
| Any single character (including numbers, space, underscores, etc.)
 +
| '''ab?d''' equals '''abcd''', '''ab1d''', '''ab d''', '''ab_d''', etc.
 +
|-
 +
| <center>[]</center>
 +
| Brackets enclose a set of characters, any one of which may match a single character at that position.
 +
| '''foo[ab]ar '''equals '''fooaar''' and '''foobar'''
 +
|-
 +
| <center>-</center>
 +
| (only within a pair of brackets) denotes a range of characters.
 +
| '''foo[a-z]ar '''equals '''fooaar''', '''foobar,''' '''foocar,''' '''foodar,''' etc.
 +
|}
  
 +
Backreferences allow for reuse of wildcard matches in the Replace field using the '''$N''' notation, where '''$1''' is the first wildcard match, '''$2''' is second, and so on.
 +
 +
Examples of the use of backreferences:
 +
 +
{| class="wikitable"
 
|-
 
|-
| <center>?</center>
+
! Find
| Any single character (including numbers, space, underscores, etc.)
+
! Replace
| '''ab?d''' equals '''abcd''', '''ab1d''', '''ab d''', '''ab_d''', etc.
+
! Example Input
 +
! Produced Output
 +
|-
 +
| <code>*, *</code>
 +
| <code>$2 $1</code>
 +
| <code>Smith, John</code>
 +
| <code>John Smith</code>
 +
|-
 +
| <code>Happy ?-th birthday *</code>
 +
| <code>$2 is $1 years old</code>
 +
| <code>Happy 5-th birthday Bob</code>
 +
| <code>Bob is 5 years old</code>
 +
|}
 +
 
 +
== Beware of conflicting replacements ==
 +
 
 +
If you enter multiple find and replace strings they will be executed as multiple Replace rules, so first string will go first and only after replacing all (or first, or last) occurrences of that string the second string in the Find field will be searched and replaced.
 +
 
 +
{|
 +
|
  
 +
{| class="wikitable"
 +
! Find
 +
! Replace
 
|-
 
|-
| <center><nowiki>[]</nowiki></center>
+
| A*{{Pipe}}*B
| Brackets enclose a set of characters, any one of which may match a single character at that position.
+
| B*{{Pipe}}*A
| '''<nowiki>foo[ab]ar </nowiki>'''equals '''fooaar''' and '''foobar'''
+
|}
 +
 
 +
|
  
 +
{| class="wikitable"
 +
! Name
 +
! New Name
 
|-
 
|-
| <center>-</center>
+
| ABBA.mp3
| (only within a pair of brackets) denotes a range of characters.
+
| AAAA.mp3
| '''<nowiki>foo[a-z]ar </nowiki>'''equals '''fooaar''', '''foobar,''' '''foocar,''' '''foodar,''' etc.
 
 
|}
 
|}
 +
 
|}
 
|}
 +
 +
You may expect the new name to be <tt>BAAB.mp3</tt>, but it's not. This happens because first all <tt>A</tt>'s are replaced with <tt>B</tt>'s (we get <tt>BBBB.mp3</tt>) and only then all <tt>B</tt>'s are replaced with <tt>A</tt>'s (and the final result is <tt>AAAA.mp3</tt>).
 +
 +
If you need to apply character-to-character mappings you should use [[ReNamer:Rules:Translit|Translit rule]].
 +
 +
[[Category:ReNamer]]

Latest revision as of 19:49, 8 August 2023

Replace Rule

ReplaceRule.png

This rule removes the specified string from the name and replaces it with another string. It has options to replace the first occurrence, the last occurrence, or all the occurrences. You can replace multiple strings at a time. You can also use wildcards to create simple patterns.

The parameters are as follows:

Parameter Details
Find Enter the string to be replaced.
  • You can enter multiple strings at a time. They will be searched & replaced in the order as they appear. Press the + button to insert a separator between two strings. Instead of pressing this button, you can also enter *|* from the keyboard.
  • If the name does not contain the specified string, the rule will not act on it.
  • If the name contains more than one of these strings, it will replace them according to the Occurrences parameter.
PlusButton.png button Inserts a separator (*|*) sequence between two delimiter entries.

In that case, the nth entry in the Find field is replaced by the nth entry in the Replace field.

Replace Enter strings that will replace the "Find" strings.

Note that the number of strings (separated with *|*) in "Find" and "Replace" fields should be the same. If there is more strings in the "Find" field than in the "Replace" field the spare strings will be removed (replaced with an empty string). If there is more strings in the "Replace" field, the spare strings will be ignored.

Insert meta tag

ReNamer Insert Meta Tag Button.png

Click the button to see a list of meta-tags.
Occurrences In case that strings occur more than once in the filename, specify which occurrences should be replaced.
Skip extension If checked, the file extension will be excluded from the processing and will remain unaffected.
Case sensitive Will only remove a specified string from the name if the case matches exactly.
Whole words only Remove the subject text only when it is whole word, not a part of another word.

For example, searching for "bar" would not find a match in "foobar" or "bars".

Interpret symbols as wild cards Treat certain symbols as Wildcards for matching simple patterns (similar to Regular Expressions).

Wildcards

Wildcard Represents Example
*
any number of characters (including numbers, space, underscores, etc.). abc* equals abc followed by 0 or more characters.
?
Any single character (including numbers, space, underscores, etc.) ab?d equals abcd, ab1d, ab d, ab_d, etc.
[]
Brackets enclose a set of characters, any one of which may match a single character at that position. foo[ab]ar equals fooaar and foobar
-
(only within a pair of brackets) denotes a range of characters. foo[a-z]ar equals fooaar, foobar, foocar, foodar, etc.

Backreferences allow for reuse of wildcard matches in the Replace field using the $N notation, where $1 is the first wildcard match, $2 is second, and so on.

Examples of the use of backreferences:

Find Replace Example Input Produced Output
*, * $2 $1 Smith, John John Smith
Happy ?-th birthday * $2 is $1 years old Happy 5-th birthday Bob Bob is 5 years old

Beware of conflicting replacements

If you enter multiple find and replace strings they will be executed as multiple Replace rules, so first string will go first and only after replacing all (or first, or last) occurrences of that string the second string in the Find field will be searched and replaced.

Find Replace
A*|*B B*|*A
Name New Name
ABBA.mp3 AAAA.mp3

You may expect the new name to be BAAB.mp3, but it's not. This happens because first all A's are replaced with B's (we get BBBB.mp3) and only then all B's are replaced with A's (and the final result is AAAA.mp3).

If you need to apply character-to-character mappings you should use Translit rule.