I was trying to manipulate a string and needed to find various parts of a string. As an example, the following is the original string:
C:\temp\tempa\target\Batch003\Images\20230127.120027\ROW\000014185386.jpg
I wanted to find the part
20230127.120027
and replace it with something else, such as
new_id_374
. I also want to change the order of the subfolder that follows the matched part. The end result should be
C:\temp\tempa\target\Batch003\Images\ROW\new_id_374\000014185386.jpg
Note that the original string can also take the form
\\mach1\temp\tempa\target\Batch003\Images\20230127.120027\ROW\000014185386.jpg
Also the "20230127.120027" is the time string and can change for different source strings, and the other sub-folder names can also be something else (so
ROW
maybe something else such as
RearRight
). The number of sub-folder levels is also not fixed. The date-time sub-folder is not guaranteed to be staying on a fixed level.
I have trouble capturing the various parts for this operation.
I am using .NET version of regular expressions (namespace
System.Text.RegularExpressions
)
What I have tried:
The regular expression I used is this.
{(?<path_begin>(?:[a-zA-Z]:|\\)\\.+)(?<matched_part>\\2023[0-1][0-9][0-3][0-9]\.[0-2][0-9][0-5][0-9][0-5][0-9]\\)(?<path_to_switch_position>[a-z][A-Z]+)\\(?<path_end>\\.+)}
I plan to replace the source string with the following:
${path_begin}\\${path_to_switch_position}new_id_374${path_end}"
However, the second part consumes the rest of the string and it failed to capture the matched_part and the path_to_switch_position. How do I skip to the place that I want to capture?