0

My apologies if this question has been asked and answered, but after exhaustive research I cannot find it.

I have a fairly large text file in which are embedded file paths with file names along with text.

The Problem:

  1. Some of these file paths are http and others are normative file paths.
  2. The normal file paths can end with any file and extension.
  3. The slash can be either / or \ in the file path.

What needs to get done: I need to find the file paths and replace them without touching the name of the actual file itself or the extension. For instance:

E:\sam\sam2\sam3\fafa.png needs to be replaced as foo\fafa.png or foo/fafa.png

However, I cannot touch nor replace any http file names..these must be excluded from the find and replace.

I have tried numerous possibilities in Regex (which I admit is a weakness in my coding skills), but nothing seems to work.

Any and all help would be appreciated and if you have time an explantion of the expression. Important note: Regex expression which works with Javascript/Node would be appreciated.

0

1 Answer 1

1

You can try this:

([a-zA-z]:(?:[\\|\/][a-zA-Z\d-_. ]+)*)([\\|\/])([a-zA-Z\d-_]+\.[a-zA-z]+)

and replace by:

`foo$2$3`;

Demo

const regex = /([a-zA-z]:(?:[\\|\/][a-zA-Z\d-_. ]+)*)([\\|\/])([a-zA-Z\d-_]+\.[a-zA-z]+)/g;
const str = `E:\\sam\\sam2\\sam3\\fafa.png
bla bla bla http://blabla\\sam\\sam2\\sam3\\fafa.png kla kla kla
zzz E:\\sam\\billings\\cricketer.png adfaffdas
asdfasdfsdf
`;
const subst = `foo$2$3`;
const result = str.replace(regex, subst);
console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.