Click here to Skip to main content
15,798,508 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:

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

Note that the original string can also take the form

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.

I plan to replace the source string with the following:

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?
Updated 14-Jun-23 5:21am
Member 15627495 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 

Member 15627495 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

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:
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