This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 3 years ago.
Let's say I have a multi-line string like this:
STARTFRUIT
banana
ENDFRUIT
STARTFRUIT
avocado
ENDFRUIT
STARTVEGGIE
rhubarb
ENDVEGGIE
STARTFRUIT
lime
ENDFRUIT
I want to search for all fruit, no veggies. I try this:
MatchCollection myMatches = Regex.Matches(tbBlob.Text, "STARTFRUIT.*ENDFRUIT", RegexOptions.Singleline);
foreach (var myMatch in myMatches)
{
Forms.MessageBox.Show(String.Format("Match: {0}", myMatch), "Match", Forms.MessageBoxButtons.OK, Forms.MessageBoxIcon.Information);
}
The problem is, instead of returning me an array of three matches, it gives me a big match encompassing the first STARTFRUIT at the beginning and the last ENDFRUIT at the end. Is there a way to "minimalize" the match search? I don't see any help in RegexOptions.
Use a non-greedy modifier (a question mark) after the quantifier:
"STARTFRUIT.*?ENDFRUIT"
^
add this
Note that the question-mark here has a different meaning here than when it is used as a quantifier, where it means "match zero or one".
Related
This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 3 years ago.
I am trying to match everything between
/* and */
And also include the in between characters.
I currently managed to create a pattern that kind of does this
\/\*(.+?)\*\/
Regex Tester
But it doesn't match multi line quotes and only matches once.
How can I improve this pattern to match everything that starts with /* and ends with */ ?
You need the RegexOptions.Singleline option, which makes the . match newlines.
Regex rx = new Regex("/\*(.+?)\*/", RegexOptions.Singleline);
This question already has answers here:
C# Code to generate strings that match a regex [closed]
(4 answers)
Closed 3 years ago.
Based off a regex string I would like to get a list of all the possible strings that would match the regex.
Example:
Given a regex string like...
^(en/|)resources/case(-| )studies/
I want to get a list of all the possible strings that would match the regex expression. Like...
^en/resources/case-studies/
or
^/resources/case-studies/
or
^en/resources/case studies/
or
^/resources/case studies/
Thank you
Note that in regex ^ denotes the beginning of the line. You must escape it
Try
\^(en)?/resources/case(-|\s)studies/
explanation:
\^ is ^ escaped.
(en)? is optionally en, where ? means zero or one times.
/resources/case the text as is.
(-|\s) minus sign or white space.
studies/ the text as is.
See: https://dotnetfiddle.net/PO4wKV
This question already has answers here:
Reference - What does this regex mean?
(1 answer)
What do ^ and $ mean in a regular expression?
(2 answers)
Closed 3 years ago.
I've already read this here and I still have got some questions. I will be very grateful if you can help me to solve them. I'm trying to compose a RegEx to verify that a string contains only letters, numbers, underscores and hyphens. Firstly, when I tried to do it (without Google-search) I did this #"[A-Za-z0-9_-]". After I made some research I found that it should be #"^[a-zA-Z0-9_-]$" where:
^ asserts position at start of a line
$ asserts position at the end of a line
My question is why we should insert these symbols? And my other question is why the string "jeffbutt" (with yellow in the screenshot) doesn't match?
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 4 years ago.
I need to get all substrings that are placed between 2 signs.
For example substrings placed between ] and [:
abcabc]substrings[kkkkkkk]iwant[12345]tohave[!##$%]
and I get: substrings iwant tohave
I tried (?<=\])(.*)(?=\[) but it returns substrings[kkkkkkk]iwant[12345]tohave.
Your regex would need to be (?<=\])(.*?)(?=\[).
Note the added ? sign to match as few as possible.
Then you have to combine the (at the moment) three matches with spaces and you will get the output you want!
Make it non greedy .*? or else it would match until the last [
You don't need the capturing group if you want to get the matches only:
(?<=\]).*?(?=\[)
Test
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 8 years ago.
I have this piece of code but have no idea what it is supposed to be matching. I have looked at many different sites to try and learn the keywords, but I just don't understand regex.
string key = #"^(.*)\s*=\s*(.*)\s*$";
Match value = Regex.Match(line, key);
This looks for the start of a line (^), finds any number of characters ((.*)), followed by some whitespace (\s*) an equal sign (=) some more whitespace (\s*) and any number of characters ((.*)) and the end of line ($)
Some valid example lines:
a=a
abc = xyz
value=5
etc