Crazenezz
Crazenezz

Reputation: 3456

Regex - Recursive Pattern

I have a string with same pattern like this:

../assets/
../../assets/
../../../assets/

I'm using regex to find the ../ pattern using:

(\.\./)

My purpose is to replace all the ../ become root/, and all the string above will become like this root/assets/

Is there a way to do that with some kind recursive pattern with regex?


Update

I'm using C#

string content1 = "../assets";
string content2 = "../../assets";
string content3 = "../../../assets";
string pattern1 = "(\.\./)";
string pattern2 = "(\.\./\.\./)";
string pattern3 = "(\.\./\.\./\.\./)";

// All the result is "root/assets"
content1 = Regex.Replace(content1, pattern1, "root/");
content2 = Regex.Replace(content2, pattern2, "root/");
content3 = Regex.Replace(content3, pattern3, "root/");

Upvotes: 1

Views: 907

Answers (1)

Amadan
Amadan

Reputation: 198496

No recursion necessary, you can just do s#(\.\./)+#root/#g (You did not specify which language, so this is Ruby/Perl version): find any number of repeated ../ and replace the whole thing with root/.

Upvotes: 2

Related Questions