In programming, a "string" is a series of text characters. Many Photo Mechanic variables contain data in the form of strings, but sometimes you may want to filter out part of that text in order to fill out another metadata field.
Substring extraction can be used to ‘extract’ a range of characters from an image variable so they can be used like normal variables in other fields throughout Photo Mechanic.
The format is: {variable:index,count:conversion} or {variable:index:conversion}. In either case, <:conversion> is optional, but the index is required. (UC, PC, and lc will not work unless you specify an index. If you are converting the entire string, use 0)<index> Sets the point in the string at which the operation starts - zero (0) will include all characters, 1 will ignore the first character, etc. This value can be positive or negative. Negative numbers count back from the end of the string, positive numbers count from the beginning. Again, the index is required.
<count> to extract <count> characters from <index>. <count> may be positive or negative. If <count> is negative then it trims off |<count>| characters from the end of the string. <count> may also be omitted altogether which indicates extracting the substring from <index> position to the end of the string. <conversion> may be one of the following types or may be omitted altogether:
"lc" | Convert applicable characters to lower-case |
"UC" | Convert applicable characters to upper-case |
"PC" | Convert to Proper Noun case |
"tr" | Translate or Find & Replace one character with another |
Examples
Let’s say we have a file named DSC_1234.JPG. So the variable for {filename} would yield DSC_1234.JPG and {filenamebase} would be DSC_1234. Then to extract substrings from this:
Variable Entry | Result |
---|---|
{filenamebase:0,3} | DSC |
{filenamebase:-4,4} | 1234 |
{filenamebase:-4} | 1234 |
{filenamebase:2,3} | C_1 |
{filenamebase:0:lc} | dsc_1234 |
{filenamebase:0:PC} | Dsc_1234 |
{filename:0,-4} | DSC_1234 |
Translate
To find and replace characters within strings use:
tr:find/repl |
Where 'find' is one or more characters to find, and 'repl' is one or more characters with which to replace found instances.
The find and replacement sequences must be separated by a forward slash character. To use a forward slash as either a find sequence or a replace sequence, it must be escaped with a backslash, like so:
\/ |
However, because of the escaping, to find or replace with a backslash, it must also be escaped, like so:
\\ |
All other sequences of characters do not need escaping. The other conversion features (lc,UC,PC) can be used as well but they must precede the translate sequence and a colon must separate the conversion sequence from the translation sequence. Example:
{caption:0:lc:tr: /_} |
The example above will convert all characters of the Caption to lower-case, and then it will translate all spaces to underscores.