Filehero wrote:In this case, "$1" is not a real "replacement"
but the back reference to the match which than will be returned instead of a replaced match?
Right.
IOW: I would say that "$1" is always the back reference, which often is used as replacement, or like here as cut-out procedure.
Explanations:
regexmatches()
Returns a
LIST of all matches of a regular expression pattern in a given string.
regexreplace()
Replaces parts of a string, using a regular expression pattern.
So with regexmatches() you just could match (for example) all timestamps to a new list (like a array)
and than pick one from that new list, or process one after the other in a foreach loop.
Code: Select all
//regexmatches(string, pattern, [separator="|"], [matchcase=0])
$result = regexmatches($input, "[\d_ ]{19}", "<crlf>");
With regexreplace() you can replace parts of the origin string to a new string, or just cut out the part you want (as I did)
Code: Select all
//regexreplace(string, pattern, replacement, [matchcase])
$result = regexreplace($line, ".+\((.+) UTC.*", "$1");
You may see now the differences more clearly that there may be different cases where you have to choose the right tool to do the job.
###################################
POC:
regexmatches()
Returns a
LIST of all matches of a regular expression pattern in a given string. (would be one single match only, in our case above)
Code: Select all
$input = <<<$$$
1) "c:\PK5_7404 (2014_09_16 19_38_20 UTC) ((Blub Bla) Bla Blub (2015_09_16 19_38_20 UTC)"
2) "c:\PK5_7404 (2016_09_16 19_38_20 ) ((Blub Bla) Bla Blub (2017_09_16 19_38_20 UTC)"
3) "c:\PK5_7404 (2018_09_16 1) ((Blub Bla) Bla Blub (2019_09_16 19_38_20 UTC)"
$$$;
//regexmatches(string, pattern, [separator="|"], [matchcase=0])
$result = regexmatches($input, "[\d_ ]{19}", "<crlf>");
msg "$input<crlf 3>$result";
Result:
Code: Select all
---------------------------
XYplorer
---------------------------
1) "c:\PK5_7404 (2014_09_16 19_38_20 UTC) ((Blub Bla) Bla Blub (2015_09_16 19_38_20 UTC)"
2) "c:\PK5_7404 (2016_09_16 19_38_20 ) ((Blub Bla) Bla Blub (2017_09_16 19_38_20 UTC)"
3) "c:\PK5_7404 (2018_09_16 1) ((Blub Bla) Bla Blub (2019_09_16 19_38_20 UTC)"
2014_09_16 19_38_20
2015_09_16 19_38_20
2016_09_16 19_38_20
2017_09_16 19_38_20
2019_09_16 19_38_20
---------------------------
OK
---------------------------
OR JUST THE LAST ONE FROM THE RESULT LIST:
Code: Select all
$input = <<<$$$
1) "c:\PK5_7404 (2014_09_16 19_38_20 UTC) ((Blub Bla) Bla Blub (2015_09_16 19_38_20 UTC)"
2) "c:\PK5_7404 (2016_09_16 19_38_20 ) ((Blub Bla) Bla Blub (2017_09_16 19_38_20 UTC)"
3) "c:\PK5_7404 (2018_09_16 1) ((Blub Bla) Bla Blub (2019_09_16 19_38_20 UTC)"
$$$;
ForEach( $line, $input, "<crlf>"){
// capture the parts from the origin string into an result string $tmp:
//regexmatches(string, pattern, [separator="|"], [matchcase=0])
$tmp = regexmatches($line, "[\d_ ]+", ",");
// get the last (" -1 ") item from the $tmp string:
//gettoken(string, [index=1], [separator=" "], [format], [flags])
$result = gettoken($tmp, -1, ",");
// example output:
msg "$line<crlf 3>$result";
}
Results:
Code: Select all
---------------------------
XYplorer
---------------------------
1) "c:\PK5_7404 (2014_09_16 19_38_20 UTC) ((Blub Bla) Bla Blub (2015_09_16 19_38_20 UTC)"
2015_09_16 19_38_20
---------------------------
OK
---------------------------
and so on...
2017_09_16 19_38_20
2019_09_16 19_38_20
------------- or:
FOR EACH ITEM FROM THE RESULT LIST:
Code: Select all
$input = <<<$$$
1) "c:\PK5_7404 (2014_09_16 19_38_20 UTC) ((Blub Bla) Bla Blub (2015_09_16 19_38_20 UTC)"
2) "c:\PK5_7404 (2016_09_16 19_38_20 ) ((Blub Bla) Bla Blub (2017_09_16 19_38_20 UTC)"
3) "c:\PK5_7404 (2018_09_16 1) ((Blub Bla) Bla Blub (2019_09_16 19_38_20 UTC)"
$$$;
ForEach( $line, $input, "<crlf>"){
//regexmatches(string, pattern, [separator="|"], [matchcase=0])
$tmp = regexmatches($line, "[\d_ ]{19}", ",");
ForEach( $index, "1,2", ","){
//gettoken(string, [index=1], [separator=" "], [format], [flags])
$result = gettoken($tmp, $index , ",");
if($result){msg "$line<crlf 3>$result";}
}
}
Results:
Code: Select all
---------------------------
XYplorer
---------------------------
1) "c:\PK5_7404 (2014_09_16 19_38_20 UTC) ((Blub Bla) Bla Blub (2015_09_16 19_38_20 UTC)"
2014_09_16 19_38_20
---------------------------
OK
---------------------------
Code: Select all
---------------------------
XYplorer
---------------------------
1) "c:\PK5_7404 (2014_09_16 19_38_20 UTC) ((Blub Bla) Bla Blub (2015_09_16 19_38_20 UTC)"
2015_09_16 19_38_20
---------------------------
OK
---------------------------
and so on...
2) "c:\PK5_7404 (2016_09_16 19_38_20 ) ((Blub Bla) Bla Blub (2017_09_16 19_38_20 UTC)"
2016_09_16 19_38_20
2) "c:\PK5_7404 (2016_09_16 19_38_20 ) ((Blub Bla) Bla Blub (2017_09_16 19_38_20 UTC)"
2017_09_16 19_38_20
3) "c:\PK5_7404 (2018_09_16 1) ((Blub Bla) Bla Blub (2019_09_16 19_38_20 UTC)"
2019_09_16 19_38_20
###################################
regexreplace()
Replaces parts of a string, using a regular expression pattern.
Code: Select all
$input = <<<$$$
1) "c:\PK5_7404 (2014_09_16 19_38_20 UTC) ((Blub Bla) Bla Blub (2015_09_16 19_38_20 UTC)"
2) "c:\PK5_7404 (2016_09_16 19_38_20 ) ((Blub Bla) Bla Blub (2017_09_16 19_38_20 UTC)"
3) "c:\PK5_7404 (2018_09_16 1) ((Blub Bla) Bla Blub (2019_09_16 19_38_20 UTC)"
$$$;
ForEach( $line, $input, "<crlf>"){
//regexreplace(string, pattern, replacement, [matchcase])
$result = regexreplace($line, ".+\((.+) UTC.*", "$1");
msg "$line<crlf 3>$result";
}
Results:
Code: Select all
---------------------------
XYplorer
---------------------------
1) "c:\PK5_7404 (2014_09_16 19_38_20 UTC) ((Blub Bla) Bla Blub (2015_09_16 19_38_20 UTC)"
2015_09_16 19_38_20
---------------------------
OK
---------------------------
and so on...
2017_09_16 19_38_20
2019_09_16 19_38_20
HTH?