String Manipulation in C #: Trim a Path Using Characters After Each Numeric Value

Given the input string, I would get a conclusion from this in the specified format filename;path.

For 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 line of output: func.js;VOBSWeb/VosWebUI/VaultWeb/func.js

The file name is indicated at the end of the entire line, and its path must be separated using characters after each numerical value (for example,, /1/VOBSWeb/and then /4/VobsWebUI, and then /2/vaultWeb)

+3
source share
3 answers

If the number of paths is arbitrary, you need a two-step approach:


First, remove all "uninteresting things" from the line.

Find .*?/\d+/([^/]+/?)and replace everything with $1.

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

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

VOBSWeb/VobsWebUI/VaultWeb/func.js.


, .

(.*/)([^/]+)$ $2;$1$2.

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

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

func.js;VOBSWeb/VobsWebUI/VaultWeb/func.js


, :

^.*?/\d+/([^/]+/).*?/\d+/([^/]+/).*?/\d+/([^/]+/).*?/\d+/([^/]+)

$4;$1$2$3$4.

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

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

, ; , JavaScript .

+1

Javascript split() , , , , , , .

0

, , :

:

.*[0-9]/([a-zA-Z]*)/[^0-9]*[0-9]/([a-zA-Z]*)/[^0-9]*[0-9]/([a-zA-Z]*)/[^0-9]*[0-9]/([a-zA-Z.]*)/

:

\4;\1/\2/\3/\4
0

Source: https://habr.com/ru/post/1758320/


All Articles