This question already has answers here:
Regex to get string between curly braces
(16 answers)
Closed 2 years ago.
How do I use regex to get what is inside of a ${} enclosed value such as:
Dont care about this. ${This is the thing I want regex to return} Not this either.
Should return:
This is the thing I want regex to return
I've tried \${ }$
the best I got messing around on regex101.com
I'll be honest I have no Idea what I'm doing as far as regex goes
using on c++ but would also (if possible) like to use in geany text editor
I suggest \${[^}]*}. Note that $ have special meaning in regular expressions and need to be escaped with a \ to be read literary.
I use [^}]* instead of .* between the braces to avoid making a long match including the entire value of:
${Another} match, more then one ${on the same line}
[^}] means anything but }
What you want is matching the starting ${ and the ending } with any amount of characters in between: \$\{.*\}. The special part here is the .*, . means any character and * means the thing in front of it can be matched 0 or more times.
Since you want thre matched results, you might also want to wrap it in (): (\$\{.*\}). The parenthesis makes regex remember the stuff inside for later use.
See this stackoverflow on how to get the results back:
How to match multiple results using std::regex
Related
This question already has answers here:
How can I match overlapping strings with regex?
(6 answers)
Matching when an arbitrary pattern appears multiple times
(1 answer)
Closed 2 years ago.
I'm trying to find-and-replace instances where consecutive commas appear throughout a string; replacing them w/ something like ",N/A,". I was using a very simple /,,/g pattern, and that works on things like ",,abc" and ",,,,abc" (with even numbers of commas). However, it doesn't catch things like ",,,abc". That's because the first two commas are considered a match, and then the third comma is just considered part of a new ",abc" string. Is there a way to handle this w/ a RegEx pattern or options? Otherwise, I'm going to need to perform multiple searches.
FWIW - I'm working in JavaScript, but I'm guessing this is just a general RegEx question/answer.
The reason why /,,/g only matches once with three commas is because the global match restarts after the position of the final consumed characters. You need a way to match the pattern of ,, without consuming those characters for pattern matching purposes.
If your language supports it, use a positive lookahead. A positive lookeahead lets a regex match some additional characters, but not consume them in the pattern.
/,(?=,)/g
In English, this means:
, # match a comma, then
(?= #start a group that must exist, and if so, isn't consumed by the pattern,
, # a comma
)
See more about this here: https://www.regular-expressions.info/lookaround.html
Javascript supports positive lookahead. :)
This question already has answers here:
JavaScript regex get all matches in a string
(2 answers)
Closed 2 years ago.
I'm trying to build a regular expression that captures anything between square brackets like the following numbers.
[phone]010101[/phone] [phone]434343[/phone]
[phone]3443434[/phone]
so the matches should be 010101, 434343, 3443434
I built cow([\s\S]*?)milk to experiment, and this seems to capture multiple matches and works fine with multiple lines, achieving what I exactly need.
However when I attempted to build the actual regex using this: \[phone\]([\s\S]*?)\[\/phone\] , it would only capture the first single match.
What could be wrong with my expression?
Another approach. This will capture the numbers as you intend.
\](.*)\[
RegexDemo
The regex is correct but global and multi-line flags are missing. In JavaScript, with g (global) and m (multiline) flags added to regex, intended matches can be found.
str=`[phone]010101[/phone] [phone]434343[/phone]
[phone]3443434[/phone]`;
reg = /\[phone\]([\s\S]*?)\[\/phone\]/gm;
[...str.matchAll(reg)].map(x=> x[1]); //["010101", "434343", "3443434"]
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
Suppose we have a string:
some random text $$ hello world $$ some more stuff
In the string delimited by $$, I would like to capture all occurrences of o (for example, in order to find and replace in Sublime Text 3).
How do I formulate such a regex command? Even though I plan to use this regex command in Sublime Text, I can take a regex command that uses different notation and fix it for use in ST3.
Use the regex:
(\$\$[^o]*)o(.*?\$\$)
replace with:
\15\2
where 5 is the substitution string.
Test here.
If you want to search for a sub-string instead of only one character, you may use:
(\$\$.*?)lo(.*?\$\$)
with the same replacement string.
Test here.
Another option is to extract first the sub-string between the delimiters ($$ hello world $$), and then on the result perform any search-and-replace action you need. This might also need looping until no replacements are done any more.
To the best of my regex knowledge, you'll need to use two regular expressions to replace all the o's between the delimiters. The first one would be to actually grab the text within the delimiter. For example:
(?P<start>\$\$) # start by grabbing the starting literal '$$'
(?<middle>.*?)(?=\$\$) # then grab everything up until the ending '$$'
(?P<end>\$\$) # now grab the ending '$$'
Example: https://regex101.com/r/015Xj8/4
Obviously you know what the start and ending is so you can simplify it further if you wanted (no start or end group for example), but I've included that for thoroughness.
Once you've captured the start/end of it, you can replace the o by a straight find/replace on the literal 'o'. As far as I know, it takes two steps via regex to do what you're after. I'm not too knowledgeable about sublime but perhaps there's a sed-like replacement feature in it.
The following will replace the first o in the string (similar to the other answer by virolino), but you'll need to click "Enter" a bunch of times to make sure that all the last o's are replaced for this to be useful:
(?P<start>\$\$)
(?<middle>.*?(?<first_o>o).*?)(?=\$\$)
(?P<end>\$\$)
Or, if you're just looking to capture O's (and nothing else), you can just make sure it's between a starting and ending $$:
\$\$.*?(o).*?\$\$
At first I thought this must be very easy, I am just overlooking something, but so far with my limited knowledge of regex I can't figure this out,
I have a regex like [some characters]MYNAME actual thing is:
rx = rx + `[ ,\t,,\,,\(,=,#,\s]+(MYNAME)`
I want this regex to also detect a line that starts with MYNAME.
So the question is, is there a way to add ^ inside [] with other things? or to OR the ^ with a [some characters]?
I can't make it work either with javascript or golang. If there are differences related to this matter, I am interested in the golang specific solutions.
You can use alternation. Also, there are some unnecessary characters in your character class:
I don't know what those commas are supposed to do? Did you intent them to act as separator? If yes, remove them.
Also, you don't need to escape ( in a character class.
Since you have added \s, you don't need to add \t and " " separately.
So, your regex can be simplified to:
"(?:[(=#\s]+|^)(MYNAME)"
This question already has answers here:
Regular expression to stop at first match
(9 answers)
Closed 1 year ago.
Let's say I have the following string:
this is a test for the sake of
testing. this is only a test. The end.
and I want to select this is a test and this is only a test. What in the world do I need to do?
The following Regex I tried yields a goofy result:
this(.*)test (I also wanted to capture what was between it)
returns this is a test for the sake of testing. this is only a test
It seems like this is probably something easy I'm forgetting.
The regex is greedy meaning it will capture as many characters as it can which fall into the .* match. To make it non-greedy try:
this(.*?)test
The ? modifier will make it capture as few characters as possible in the match.
Andy E and Ipsquiggle have the right idea, but I want to point out that you might want to add a word boundary assertion, meaning you don't want to deal with words that have "this" or "test" in them-- only the words by themselves. In Perl and similar that's done with the "\b" marker.
As it is, this(.*?)test would match "thistles are the greatest", which you probably don't want.
The pattern you want is something like this: \bthis\b(.*?)\btest\b
* is a greedy quantifier. That means it matches as much as possible, i.e. what you are seeing. Depending on the specific language support for regex, you will need to find a non-greedy quantifier. Usually this is a trailing question mark, like this: *?. That means it will stop consuming letters as soon as the rest of the regex can be satisfied.
There is a good explanation of greediness here.
For me, simply remove /g worked.
See https://regex101.com/r/EaIykZ/1