PowerShell regex to filter values between {{ and }} - regex

I'm trying to write a regex to filter the values, that exist between curly braces:
Example: $path = C:\serices\ApplicationName\{{NewFolderName}}
I want to filter the value "NewFolderName" from the above variable. Once I execute, I want to see "NewFolderName" in my output. I'm using PowerShell version 2.0. Can someone please suggest me the possible solution.
Thanks in advance.

One way would be:
\{{2}(.*?)\}{2}
See a demo on ideone.com.

Here's the full powershell for your question.
$path = "C:\users\{{XXX}}\Downloads\{{YYY}}\AppName"
$matches = [regex]::Matches($path,"\{{2}(.*?)\}{2}").value

Related

RegEx remove part of string and and replace another part

I have a challenge getting the desired result with RegEx (using C#) and I hope that the community can help.
I have a URL in the following format:
https://somedomain.com/subfolder/category/?abc=text:value&ida=0&idb=1
I want make two modifications, specifically:
1) Remove everything after 'value' e.g. '&ida=0&idb=1'
2) Replace 'category' with e.g. 'newcategory'
So the result is:
https://somedomain.com/subfolder/newcategory/?abc=text:value
I can remove the string from 1) e.g. ^[^&]+ above but I have been unable to figure out how to replace the 'category' substring.
Any help or guidance would be much appreciated.
Thank you in advance.
Use the following:
Find: /(category/.+?value)&.+
Replace: /new$1 or /new\1 depending on your regex flavor
Demo & explanation
Update according to comment.
If the new name is completely_different_name, use the following:
Find: /category(/.+?value)&.+
Replace: /completely_different_name$1
Demo & explanation
You haven't specified language here, I mainly work on python so the solution is in python.
url = re.sub('category','newcategory',re.search('^https.*value', value).group(0))
Explanation
re.sub is used to replace value a with b in c.
re.search is used to match specific patterns in string and store value in the group. so in the above code re.search will store value from "https to value" in group 0.
Using Python and only built-in string methods (there is no need for regular expressions here):
url = r"https://somedomain.com/subfolder/category/?abc=text:value&ida=0&idb=1"
new_url = (url.split('value')[0] + "value").replace("category", 'newcategory')
print(new_url)
Outputs:
https://somedomain.com/subfolder/newcategory/?abc=text:value

Regex in Notepad++ to move contents of an element to an attribute value

I'm trying to solve a regex riddle. Let's say I have rows of hrefs looking like this:
anchor1.in
an3.php
setup.exe
What I want the regex (or any other solution) to do is to take the href title and copy it over to the actual url with a foward slash in front of it.
A successful result would become:
anchor1.in
an3.php
setup.exe
If you can solve this please explain how you did it.
You can use the following to match:
(<a\s+href=")(.*?)(">)(.*?)(<\/a>)
And replace with:
\1\2/\4\3\4\5
See DEMO and Explanation

regular expression : get super scripted text

I would like to get super scripted text via following html string.
testing to <sup>supers</sup>cript o<sup>n</sup>e
The result I would like to get is like below
supers
n
This is what I tried right now
But the result is not what I want.
<sup>supers
<sup>n
Could anyone give me suggestion please?
You can use lookbehind in your regex:
(?<=<sup>)[^<]*
Update Demo
Use this if there may be other HTML tags between <sup> and </sup>:
(?<=<sup>)(.*?)(?=<\/sup>)
Check the demo.
You were close, just not capturing your match:
Updated regex
(?:<sup>)([^<]*) I just added a capture group around your match
(?<=<sup>)([^<]*?)(?=<\/)
This should work.
See demo.
http://regex101.com/r/sA7pZ0/13

Regular Expression to print only executable not full path

I have a set of key/value pairs that lists executables using the fully qualified name as follows:
["exec"] = "/share/home/00288/tg455591/NAMD_2.8b3/NAMD_2.8b3_Linux-x86_64-MVAPICH-Intel-Ranger/namd2",
However, I would like to take out the directory location and only print the executable, such as the following:
["exec"] = "namd2",
How can I construct a regular expression to make this change? Keep in mind all the executables in the file are different, but they have similar format. The size of the file is approximately 6000 lines. Thanks in advance.
This regular expression did the trick:
(?<=\["exec"\] = ").+?(?=[^/]+?(?:\.\w+)?")
Replace with:
\1\2
try this to find :
(?<="exec"] = ")[^"]*?/([^/"]+)(?=")
and this to replace :
\1
(or $1)
If I understand the problem correctly, this sed command will do the trick:
sed 's|= .*/|= "|'

A regular expression to exclude a word/string

I have a regular expression as follows:
^/[a-z0-9]+$
This matches strings such as /hello or /hello123.
However, I would like it to exclude a couple of string values such as /ignoreme and /ignoreme2.
I've tried a few variants but can't seem to get any to work!
My latest feeble attempt was
^/(((?!ignoreme)|(?!ignoreme2))[a-z0-9])+$
Any help would be gratefully appreciated :-)
Here's yet another way (using a negative look-ahead):
^/(?!ignoreme|ignoreme2|ignoremeN)([a-z0-9]+)$
Note: There's only one capturing expression: ([a-z0-9]+).
This should do it:
^/\b([a-z0-9]+)\b(?<!ignoreme|ignoreme2|ignoreme3)
You can add as much ignored words as you like, here is a simple PHP implementation:
$ignoredWords = array('ignoreme', 'ignoreme2', 'ignoreme...');
preg_match('~^/\b([a-z0-9]+)\b(?<!' . implode('|', array_map('preg_quote', $ignoredWords)) . ')~i', $string);
As you want to exclude both words, you need a conjuction:
^/(?!ignoreme$)(?!ignoreme2$)[a-z0-9]+$
Now both conditions must be true (neither ignoreme nor ignoreme2 is allowed) to have a match.
This excludes all rows containing ignoreme from search results. It will also work pretty well when there are any character in a row
^((?!ignoreme).)*$
This worked for me:
^((?!\ignoreme1\b)(?!\ignoreme2\b)(?!\ignoreme3\b).)*$
This worked for me in python 3.x for a Machine Learning pipeline make_column_selector for including and excluding certain columns from a dataframe. to exclude ^(?!(col2|co4|col6)).*$
categoral_selector = make_column_selector(pattern = "(col2|co4|col6)")
numeric_selector = make_column_selector(pattern = "^(?!(col2|co4|col6)).*$")
simpler:
re.findall(r'/(?!ignoreme)(\w+)', "/hello /ignoreme and /ignoreme2 /ignoreme2M.")
you will get:
['hello']