Click here to Skip to main content
16,016,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Posted
Updated 14-Jun-23 4:21am
v7
Comments
[no name] 13-Jun-23 15:08pm    
Hello !

about Path and regexp .. it exists a classy code to achieve without Regexp.

var _path = "the path"
var _path_in_array = _path.split("/")

// path_in_array will contains, with indexes, all the sub part of your path.

// and to group again your path in a full one : the join() function
var final_path = path_in_array.join()

// look at this two function in .Net Help pages. for /split/ and /join/
// It could solve your question 

[no name] 14-Jun-23 1:22am    
one details about paths and their Length... and to avoid any length bug :
- use the "Path." functions provided by .Net libraries.

because a common bug between short Pathes, and New wide paths could happens.


Paths are not usual String when used by your software.

I modified the regular expression in my question. The new one is
(?<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-zA-Z]+)\\(?<path_end>.+)

It does what I wanted it to do. The key change is the non-greedy matching specified by the question mark after the '+'.
 
Share this answer
 
If the Part to the right of the string you want is always ROW/filename.ext then I'd use that and Regex.Replace:
RegEx
(\\\d*\.\d*)(?=\\.+?\\..*\..{3})
Should do it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900