I'd like to search for filenames that contain two hyphens (only). Some filenames have one hyphen, I just want the one's with two hyphens in the name:
THIS: some text - more text - yet more.txt
NOT THIS: some text - more text.txt
The hyphens are always surrounded by a space, FWIW.
I tried using (.*) - (.*) - (.*) and a couple variants, but the results aren't what I am looking for. I either get nothing or filenames with just one hyphen when I try various combinations.
I know this is an obvious one, but I have tried wading through regex tutorials concerning greedy, look aheads, etc. but can't for the life of me solve this. Can anyone help? I'm not looking for just the solution--I'd like to understand what I'm doing wrong in the regex syntax.
You can use this regex,
^[^-]*(?:-[^-]*){2}$
This when written in expanded form will look like this,
^[^-]*-[^-]*-[^-]*$
Which is how you wanted it, but I've compacted it by using quantifier to restrict the occurrence of hyphen to just two only.
Demo
If you want to extend your regex, just change .* to [^-]* to make your regex this, otherwise .* will match additional hyphens too leading to unexpected match results.
^([^-]*) - ([^-]*) - ([^-]*)$
Notice you should use start ^ and end $ anchors to make the filename match whole regex.
Demo with your modified regex
Related
I have lines like this
TEL;TYPE=CELL:343-454-1212
TEL;TYPE=CELL:34345-121212
TEL;TYPE=CELL:123456789
I need to remove dashes from them using VSCode.
So far I came with:
Search With : (TEL;TYPE=CELL:\d*)-(\d*)
Replace With : $1$2
I have to search and replace multiple times (In this case two times) to get the expected output. This is mainly because I do not know how many dashes are there.
Is there any regex which I can use to accomplish, what is being done here in single go?
Wrap the number in parenthesis like this so you can restore it, removing the -
-([0-9])
replace with $1
Or
Just repeat this find replace, till they are all removed:
find: (TEL;TYPE=CELL:[^-]*)-
replace: $1
I tried this limited solution.only for 2 or 3 dash.
Search with: (\d+)-(\d+)(?:-(\d+))?
Replace with: $1$2$3
but in the comment that said replace dashes with empty str also a good solution.
Visual Studio Code search and replace in the current document feature (not the one to replace in files) supports regexps with variable-width lookbehind patterns, so you can use
(?<=TEL;TYPE=CELL:[\d-]*)-
See the regex demo. Details:
(?<=TEL;TYPE=CELL:[\d-]*) - a position that is preceded with TEL;TYPE=CELL: and then zero or more digits or hyphens
- - a hyphen.
I am trying to write regex which should ignore any string ends with _numbers like (_1234)
like below
abc_def_1234 - should not match
abc_fgh - match
abc_ghj - match
abc_ijk_2345 - not match
I am trying to use lookahead regex like below, but it's matching everything. Can someone please help me how I can achieve this?
\w+(?!_\d+)
Match words separated by underscores, but use a negative look ahead to exclude input that has the unwanted tail:
^(?!.*_\d+$)\w+(?<!_)$
See live demo.
The last look behind (which you can remove) is there to require that the last char is not an underscore - ie that the input is AFAICT well formed.
I am trying to regex & extract some URLs from a large text file. Most URLs don't have an HTTP/HTTPS affixed to them so it is making this a lot more difficult.
If I want to regex for URLs containing ".co", I made it so the regex finds ".co" and selects from the first space before the occurrence and to first space after the occurence using:
(\S+\.co\S+)
But the problem with this comes when I have URLs with the .com TLD in the file too.
For example, this regex selects all URLs from below instead of only the ".co" URLs
pizza.com/test is good
pizza.co/test is great
Regex Extracts:
pizza.com/test
pizza.co/test
I only want it to extract:
pizza.co/test
Here is my regexr example: https://regexr.com/5hl2h
Does anyone know of a way I can achieve this with regex? Or should I look for an alternative solution?
Much appreciate the help here.
You could use
\S+\.co(?!m)\S*
Explanation
\S+ Match 1+ non whitespace chars
\.co(?!m) Match .co not directly followed by m
\S* Match 0+ non whitespace chars to also match ending with .co
Regex demo
I'm trying to use regex_replace in prestashop product_list.tpl. My code is like:
{$product.description|regex_replace:".*(?=Kompatybilny)":""|strip_tags:'UTF-8'}
I'd like it to show $product.destription after "Kompatybilny" word, but it doesn't work and I don't know why. I've tried different regex functions but still the same - variable doesn't show at all.
You may use
{$product.description|regex_replace:"/.*?(?=Kompatybilny)/su":''}
The regex will match
.*? - any 0+ chars, as few as possible, up to (but excluding from the match) the first occurrence of
(?=Kompatybilny) - the Kompatybilny substring
su - s means . can match linebreak chars and u supports Unicode strings.
I want to find and replace a bunch of links, except I don't know how to format the search expression. I want to find the links that look like this:
http://website.com/test.php?id=930&name=hello
The 930 and hello are the variables that are different.
edit: I tried "http://website.com/test.php?id=.*&name=.*"> but it selects a bunch of stuff after that too like img src on that line etc
I tried using [^"]* or (.*?) instead of .* but it says it can't find the text :(
Notepad++ seems to have limited regex. It doesn't use the nongreedy variants (.*?) and some anchors for word boundaries like \b, \Z are also not working.
I think this will help you:
http://website.com/test.php?id=\d*&name=\w*
insted of searching for .* which will also match to whitespaces, use \d* this will find only numbers and \w*, this will find only letters.
If the name can contain other things than letters than use this
http://website.com/test.php?id=\d*&name=[\w\d]*
and add into the [] all you need to match. in my example it will match letters and numbers.
Hint for future questions: Think about your tags. If you would have used the tag "regex", you would have gotten 5 answers within minutes for your question.