Regex to select path till a folder name - regex

Given a string "C:\Tom\Dick\Harry\Chocolate\Treat\Hunt\Fruitless" I have to select anything which appears before Treat.
I have tried with
(.*)\\Treat
but it includes the Treat word also.
Result is "C:\Tom\Dick\Harry\Chocolate\Treat".
Any help will be much appreciated.

You could use a lookahead in the regex if you don't want to include the word \Treat.
.*(?=\\Treat)
DEMO
OR
If you want to include the word Treat then try the below regex,
^.*?\\Treat
DEMO

(.*?)(?:Treat).*
This simple re should do it.
See demo

Related

Regex with exception of specific word of path

I need to replace image URL with a dummy image URL. I'm currently having a problem to exclude paths that have ignore file name.
I've successfully implemented regex that match these two paths:
images/image-filename.png and ../images/image-filename.png
with this following regex:
..\/images\/(.*?)\.(?:png|jpg|jpeg|gif|png|svg)|images\/(.*?)\.(?:png|jpg|jpeg|gif|png|svg)
However, I'd like to exclude any path with ignore word in the file name, for example:
images/image-filename-ignore.png
Thanks!
Here is one option using a negative lookahead to assert that ignore does not appear as part of the filename:
images\/(?!.*ignore.*\.[^.]+).*\.(?:png|jpg|jpeg|gif|png|svg)
Demo
But, you might also be able to proceed by actually matching the invalid filenames with ignore, and then logically excluding these matches:
images\/.*ignore.*\.(?:png|jpg|jpeg|gif|png|svg)
My guess is that you may likely want to add an i flag and word boundaries:
\/?images\/(?!.*\bignore\b)[^.]*\.(?:png|jpe?g|gif|svg|tiff|other_extensions)
If you wish to explore/simplify/modify the expression, it's been
explained on the top right panel of
regex101.com. If you'd like, you
can also watch in this
link, how it would match
against some sample inputs.

Use regex to analyze path in Makefile

I need to do some replacement of the path in Makefile.
For example, I have a path like this
p := some/path/to/file
I would like to do the replacement of "p" to get this
../../../file
In other words, I'd like to replace the path to relative path. How can I use regex to do that in Makefile? Any comments would be appreciated. Thanks!
Depending on software/language you are using to edit just replace any word ending with "/" to ".../"
This way you will get your relative path. Live demo here.
For special characters you can wrap everything in [] and add special characters you are expecting, for example like this. Some need escaping with \, just give it a try on Regex101 to find out which ones.
If you also have digits in your filepath you need to add \d to the "pool": demo.

regex to remove everything after the last dot in a file

I'm trying to find a regex for removing everything after the last dot in a file. So far I've found ways to remove all text before the first dot, but I can't seem to find a way to select the end of the file. Could you help me on the way?
Thanks in advance
You can try something like:
\.[^.]*$
to match everything including and after the last dot. If you don't want to include the last dot, then you can use a positive lookbehind:
(?<=\.)[^.]*$
Try following regex for search and replace
s/\.[^.]*$/\./
On Bigquery, r'([^.]+).?$' works, if you want to remove the last dot.

Regex to extract specific words

I am looking for regex to extract following words from text. For example, the word which comes after "Replaced" in each of these lines:
Replaced disk
Replaced floppy
Replaced memory
Please suggest the regex for it.
We can't really help you without more details (like which regex flavor you're using, for example), but you probably want to match it with something like this:
Replaced\s+(\w+)\b
...and then extract the desired portion from capturing group #1.
use this regex (?<=Replaced\s*)(.+)
I think
/Replaced\s(\w*?)/
Or:
/(?<=Replaced\s)(\w*)/
If you only want to select the word and not Replaced.

Regex to include some files but with one exception

I would like a regex that includes all filenames with a certain ending ex. ".err" but not if this filename starts with e.g. "test". In other words include "*.err"-files but not "test-whatever.err"-files.
I have found that
(?!test.*\.err$).*\.err
excludes the test*.err files and that
.*\.err
includes all the *.err files, but I need them both in the same expression.
Also the fact that the ".err" can be written as ".ERR" or ".Err" must be taken into concideration for this regex to work properly for me.
All thoughts and ideas are appreciated!
Regards
Rickard
Use this
^(?i)(?!test).*\.err$
See it here online on Regexr
The important parts, that are different to yours:
Use anchors. ^ and $ are anchoring your pattern to the start and to the end of the string
(?i) makes it "ignorecase", so that err will also match "ERR" or "ErR" and test will also match "Test" and TEST ...
You didn't gave the language, but this features should work with the most flavours.
How about this one:
^(?!test).*\.err$