Difference between revisions of "ReNamer:Rules:RegEx"

From den4b Wiki
Jump to navigation Jump to search
(added syntax hint menu)
(Added "BackReference explanations")
Line 32: Line 32:
  
 
'''Tip:''' ReNamer users have posted many RegEx patterns at the [http://www.den4b.com/forum/ User Forum]. You can copy and use them.
 
'''Tip:''' ReNamer users have posted many RegEx patterns at the [http://www.den4b.com/forum/ User Forum]. You can copy and use them.
 
+
<BR>
 +
<BR>
 +
== BackReference ==
 +
Replace-Hint: Use $1...$9 to reference subexpressions.
 +
<BR>
 +
<BR>
 +
'$n' refers to 'BackReference' of RegularExpression search&replace,<BR>
 +
where you can group a search pattern in parentheses (...) <BR>
 +
and reuse --what was matched and stored-- later on in replacement.<BR>
 +
<BR>
 +
'$n' would then be used in form of '$1', '$2', '$3',... and so on till '$9', <BR>
 +
counting the opening parentheses '(' from the left of the search string.<BR>
 +
Counting '(' from the left includes nested parentheses: ( 1 xxx (2 dd)(3 ff))<BR>
 +
<BR>
 +
<BR>
 +
Example on string "Vacation 2019.ext" <BR>
 +
Search Expression: "^(\w+) (\d+)\.(\w\w\w)" <BR>
 +
Replace: "$2 - $1.$3<BR>
 +
Results in: "2019 - Vacation.ext"<BR>
 +
<BR>
 +
A little bit to complicated, as you also could use "[x]Skip Extension", instead of "\.(\w\w\w)" and .$3,<BR>
 +
but just for example that may be fine to have something to explain.<BR>
 +
<BR>
 +
<BR>
 +
In this simple constellation this will work fine, and the two parts are swapped and a hyphen is added in between as we told in the replacement.<BR>
 +
For more complex issues you may make yourself more familiar with the RegularExpression syntax (see links above), or ask for help in the forum.<BR>
 +
<BR>
 +
<BR>
 +
Please note that replacement with both '$1' and digit '0' ( '$10' ) could be troublesome, as the engine may look for a tenth subexpression.<BR>
 +
Maybe use a workaround like '$1_0' and replace the underscore in a second step.<BR>
 +
<BR>
 +
<BR>
 +
There is also a second meaning/use of 'BackReference' in an Search expression: "(The )$1" for to search for two successively 'The 's.
 +
<BR>
 +
<BR>
 +
<BR>
 +
<BR>
 
[[Category:ReNamer]]
 
[[Category:ReNamer]]

Revision as of 10:33, 13 March 2019

RegExRule.png

This rule finds text that matches the specified RegEx pattern, and replaces it with another string. RegEx is short for Regular Expressions, which stands for special syntax for describing search and replace patterns. Regular Expressions are very powerful and they are really worth learning. The RegEx syntax is explained in the appendix.

Note: The TRegExpr RegEx engine used by ReNamer is a little different from the standard PERL RegEx or Windows RegEx. You may check the correct syntax in Regular Expressions section.

The parameters are as follows:

Parameter Details
Expression RegEx pattern to match or find.
Replace RegEx pattern that replaces the found pattern.
Skip extension If this check box is selected, the extension will be ignored by the rule.
Case-sensitive If this option is selected, ReNamer will search for the text in case-sensitive manner.

A simple set of commonly used RegEx syntax patterns is provided in the hint menu:

RegExRuleSyntaxHint.png

Tip: ReNamer users have posted many RegEx patterns at the User Forum. You can copy and use them.

BackReference

Replace-Hint: Use $1...$9 to reference subexpressions.

'$n' refers to 'BackReference' of RegularExpression search&replace,
where you can group a search pattern in parentheses (...)
and reuse --what was matched and stored-- later on in replacement.

'$n' would then be used in form of '$1', '$2', '$3',... and so on till '$9',
counting the opening parentheses '(' from the left of the search string.
Counting '(' from the left includes nested parentheses: ( 1 xxx (2 dd)(3 ff))


Example on string "Vacation 2019.ext"
Search Expression: "^(\w+) (\d+)\.(\w\w\w)"
Replace: "$2 - $1.$3
Results in: "2019 - Vacation.ext"

A little bit to complicated, as you also could use "[x]Skip Extension", instead of "\.(\w\w\w)" and .$3,
but just for example that may be fine to have something to explain.


In this simple constellation this will work fine, and the two parts are swapped and a hyphen is added in between as we told in the replacement.
For more complex issues you may make yourself more familiar with the RegularExpression syntax (see links above), or ask for help in the forum.


Please note that replacement with both '$1' and digit '0' ( '$10' ) could be troublesome, as the engine may look for a tenth subexpression.
Maybe use a workaround like '$1_0' and replace the underscore in a second step.


There is also a second meaning/use of 'BackReference' in an Search expression: "(The )$1" for to search for two successively 'The 's.