RegEx for ${any_text} [duplicate] - regex

This question already has answers here:
Replace all text between braces { } in Java with regex
(5 answers)
Closed 4 years ago.
I'm trying to find any solution to extract all variables definition from freemaker template. Lets assume that template looks like this:
Hello ${test_1} Hello ${test_2} Hello ${test_3} Hello ${test_4}
At the output I want to have list of test_1, test_2, test_3, test_4. I tried almost every approach but this $ (special character sign) is causing me a lot of problems also important thing (that also cause me problem) are variables that relate for objects for example ${test_1.user.name}

Thanks for dawg user the solution for this looks like:
\$\{([\w.]+)\}
One more time than you very much.
Demo

Related

Find all occurrences of an expression in a file, even on the same line [duplicate]

This question already has an answer here:
Find a string between two characters with grep
(1 answer)
Closed 2 years ago.
I have a file that has to be tailored; It contains "variables" that are represented by #variable.name#;
There may be more than one such variable on a line; and I want to find all of them.
My thought is that this should be possible with a sed regex, but most linux commands are at my disposal...
The regex I thought of was this one:
s#(.*)(#.*#)(.*)#\2#
However, that did not catch all...
Lets assume this file:
Dear #sirmadam#,
Today is #day#-#month#-#year#, a nice #weekday#day.
It is my #pleasureornot# to tell you youre #hiredorsacked#.
Sincerely,
#nameoftheboss#
The output should be:
#sirmadam#
#day#
#month#
#year#
#weekday#
#pleasureornot#
#hiredorsacked#
#nameoftheboss#
I think my main issue is that the start and end of each variable uses the same sign, but there is little I can do about that... My regex-fu is lacking... I cant wrap my head around how this could be solved, it is a bit of a puzzle for me.
grep works:
grep -o '#[^#]*#' foo.txt
By contrast, sed fundamentally works in a line-based fashion, and breaking out of this paradigm, while occasionally possible, is hard and usually indicates that another tool is better suited.

build a string that excludes text containing two specific words [duplicate]

This question already has answers here:
Regular expression to match strings that do NOT contain all specified elements
(2 answers)
Closed 2 years ago.
I am trying to find the inverse of the following string but with no success:
(?i)(?s)^(?=.*?word1)(?=.*?word2)
I built this but it is for one word only and it does not even function properly, considering that if I test it with regex101 I got one match while I should get no match:
(?i)(?s)(?!.*?word2)^.*$
Please see the following link: https://regex101.com/r/qS7yN9/72
Hope you guys can help to build the right string.
You can effectively negate the result by wrapping a negative look-ahead assertion around the whole thing:
(?i)(?s)^(?!(?=.*?word1)(?=.*?word2))

How to use multi words un 1 variable? [duplicate]

This question already has answers here:
std::cin input with spaces?
(8 answers)
Reading string with spaces in c++
(3 answers)
Closed 6 years ago.
I am trying to asign more than one words to a variable, at once, but after inserting space, it looks for another variable. Lets say I need to write Hello c++ world in a variable. The onliest solution I've found, is the following C++ code:
#include <iostream>
using namespace std;
int main()
{
string word1,word2,word3;
cin>>word1>>word2>>word3;
return 0;
}
Ok, but what if you dont know how much words is the sentance going to be?
Example entry:
say Hello there!
Example print: Hello there
Example entry: say This is much longer, we can't create milions of variables!
Example print: This is much longer, we can't create milions of variables!
Please help! What I need here is not only use spaces in a variable, I want before this to have another "command" like in the case above: the command say.

I can't ever get Regex Right [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 6 years ago.
I have a query string I'm trying to remove a query string parameter. What I currently have is
date=(.*)&
But this will remove anything to the last &. I only want to remove to the first occurance of the &.
So this is how it should work:
date=5%2F13%2F16&tab=1&a=b = tab=1&a=b
But mine is doing this:
date=5%2F13%2F16&tab=1&a=b = a=b
I think I probably need something around the & but I read the regex stuff and I just get more confused.
Make it lazy, use:
date=(.*?)&

Regular Expressions AND [duplicate]

This question already has answers here:
Regular Expressions: Is there an AND operator?
(14 answers)
Closed 6 years ago.
I am really not good with regular expressions and I come here for some assistance :). I am trying to combine regular expressions with something like AND. For example if we have a text file with:
abc1-xyz
abc1-ertxyz
abc1xyz
postxyz
abc1
I would like to match everything that starts with "abc1" AND also contains the letters "xyz" somewhere.
I know that I can start with:
/^abc1/
but I am not sure how to combine so it can also match to contain "xyz".
Thank you for your assistance in advance.
You should tell us with which language you are coding, regex engines are not always the same.
There is another ambiguous point : Do you need your string to CONTAIN xyz or to END WITH?
Considering you are coding on Javascript..
If you want it to contain xyz, try :
/^abc1.*xyz/
If you want it to end with xyz, try :
/^abc1.*xyz$/