1

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/");
4

1 回答 1

2

不需要递归,您可以这样做s#(\.\./)+#root/#g(您没有指定哪种语言,所以这是 Ruby/Perl 版本):找到任意数量的重复../并将整个内容替换为root/.

于 2013-07-17T00:28:04.927 回答