views:

96

answers:

2

Given an input string, I would like to get the output from this in the specified format: filename;path.

For the input string:

/vob/TEST/.@@/main/ch_vobsweb/1/VOBSWeb/main/ch_vobsweb/4/VobsWebUI/main/ch_vobsweb/2/VaultWeb/main/ch_vobsweb/2/func.js

I expect this output string:

func.js;VOBSWeb/VosWebUI/VaultWeb/func.js

The filename is listed at the end of the whole string, and its path is supposed to be stripped using the characters after each numeric value (eg. /1/VOBSWeb/ and then /4/VobsWebUI and then /2/vaultWeb)

Related:
This is related to an earlier C# question, but this time using JavaScript: String manipulation

A: 

Never mind ,this has been answered in an earlier post by Tim

First, remove all the "uninteresting stuff" from the string.

Search for .*?/\d+/([^/]+/?) and replace all with $1.

In C#: resultString = Regex.Replace(subjectString, @".*?/\d+/([^/]+/?)", "$1");

In JavaScript: result = subject.replace(/.*?\/\d+\/([^\/]+\/?)/g, "$1");

This will transform your string into VOBSWeb/VobsWebUI/VaultWeb/func.js.


Second, copy the filename to the front of the string.

Search for (.*/)([^/]+)$ and replace with $2;$1$2.

C#: resultString = Regex.Replace(subjectString, "(.*/)([^/]+)$", "$2;$1$2");

JavaScript: result = subject.replace(/(.*\/)([^\/]+)$/g, "$2;$1$2");

This will transform the result of the previous operation into func.js;VOBSWeb/VobsWebUI/VaultWeb/func.js

Thanks

Raj
+1  A: 

Since I almost finished typing it in anyway, here's the condensed version:

result = subject.replace(/\/?(?:[^\/\d]+\/)+\d+\/([^\/]+\/?)/g, "$1")
                .replace(/^.*\/([^\/]+)$/, "$1;$0");
Alan Moore