This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 5 years ago.
I have run into a problem when trying to find multiple matches in a length of text. The text has the following format:
string text = "#IDENTIFIER http://www.link1.com #IDENTIFIER http://www.link2.org #IDENTIFIER http://www.link3.com #IDENTIFIER http://www.link4.net";
My objective is to extract each #IDENTIFIER link occurrence from this and I am doing it using the following code:
string pat = #"(#IDENTIFIER)(.*)\.(com|org|net)";
MatchCollection matches = Regex.Matches(text, pat);
foreach(Match match in matches) {
Console.WriteLine("'{0}' found at index {1}.", match.Value, match.Index);
}
The problem is, it returns one match and not 4. Why are the in-between patterns ignored?
Do you know what I am missing?
It's because .* on its own is generally greedy. Instead try using .*?:
string pat = #"(#IDENTIFIER)(.*?)\.(com|org|net)";
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:
Regex Match.Value returning entire value, not the matched groups
(3 answers)
Closed 4 years ago.
There is a question here:
Regex Match.Value returning entire value, not the matched groups
But my question is different
I use
var match = _regex.Match(inputString);
return match.Value;
And I would like to cope without referring to groups by index. Is it possible?
I would like to have a match which does not return
#WORD from string #WORD "SOMEWORD"
Pattern: ^#WORD(.+)$
Input: #WORD "SOMEWORD"
returns #WORD "SOMEWORD"
I need only "SOMEWORD"
you can use
(?<=^#WORD ")\w+(?="$)
Regexr
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 answers here:
What special characters must be escaped in regular expressions?
(13 answers)
How to make a regex match case insensitive?
(1 answer)
Closed 5 years ago.
I am trying to do a find replace in a string of text. I am using Regex like this:
Regex regexText = new Regex("Test.Value");
strText = regexText.Replace(strText, value);
In this example I am trying to find the string "Test.Value" in a text string. However if this value appears in the string the replace does not happen.
If I remove the dots eg:
Regex regexText = new Regex("TEST");
strText = regexText.Replace(strText, value);
If I put the word "TEST" in the string, it replaces it just fine.
Is there a way to get this to work with strings with "."'s in?
You have to escape the dot:
Regex regexText = new Regex(#"Test\.Value");
As you wrote it, the regex is just looking for "Test", followed by any character except a line feed, followed by "Value".
On the top of that, if the text you are looking for is a little bit different, a case insensitive matching could help you out:
Regex regexText = new Regex(#"Test\.Value", RegexOptions.IgnoreCase);
Anyway, in this case I don't think a Regex is necessary. A simple string replace should do the job:
strText.Replace("Test.Value", value);
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".