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"]
Related
This question already has answers here:
Regex Match all characters between two strings
(16 answers)
RegEx match open tags except XHTML self-contained tags
(35 answers)
Closed 2 years ago.
I would like to mark all strings between 2 strings with the regular expression.
Example:
https://regex101.com/r/Etfpol/1
I want regular expression to mark follow text:
Solution changed from
Resolved Time changed
Updated By changed from
enter image description here
Thanks
You can use positive lookbehind and positive lookahead to check the tags.
(?<=<Name>Description<\/Name><Value>).*?(?=<\/Value>)
Match results
Solution changed from
Resolved Time changed
Updated By changed from
If you prefer not to use them, this will work as well, but the full match will include the strings before and after your desired string.
(?:<Name>Description<\/Name><Value>)(.*?)(?:<\/Value>)
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:
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
This question already has answers here:
Is it possible to check if two groups are equal?
(2 answers)
Closed 5 years ago.
I'm trying to match iff two capture groups are the same. I could manually check after the match, but I'm wondering if there is a way I can do this in the expression itself.
My expression is (\d+)\/(\d+), so I only want to accept strings where the two numbers are equal. Is there a nice way to check this in the regular expression, or do I have to manually check groups after?
EDIT: This was marked a duplicate but the supposed duplicate question is not related and does not in any way answer my question...
You can use this one in python : \b(\d+)\/+\1\b
Demo
This is the same usecase as checking for doubled words
When editing text, doubled words such as "the the" easily creep in. Using the regex \b(\w+)\s+\1\b in your text editor, you can easily find them. To delete the second word, simply type in \1 as the replacement text and click the Replace button.
Source
I assume you don't have any other capture groups, based on that:
\b(\d+)\/(\1)\b
Regex Demo
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 6 years ago.
I have a file where the application is configured to check the following Regex
[\x00-\x1F\x7F&&[^\x0A]&&[^\x0D]]
Can anyone please tell me the meaning of this regex expression exactly what it means. I do know that this regex expression ignored line feed and character feed. I even validated my file on http://regexr.com/ with the above specified regex expression and it shows no match found so not understanding why the regex is getting matched in the application.
FYI: I do not want the regex to match file as it is stopping my processing.
It could be that in Java and Ruby the regex expression && refers to character class intersection, while http://regexr.com/ doesn't support that expression and is trying to match literal & symbols. The regex you posted means match any characters from \x00 to \x1f or \x7f as long as it's not \x0A or \x0D.