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
Related
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 an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
Is there a way to verify with Regex that a string matches a patter like so:
1.001
1.002
15.001
Where the pattern is:
any number followed by
dot character
followed by three (3) numbers
Given that patters things like this would fail:
A.001
1.0001
1,001
...
Any help is appreciated.
This regex should work:
^\d+\.\d{3}$
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:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 8 years ago.
Say I have the following string:
test tester tested testing
I wish to match every word that begins with test, but not the word tester. The pattern test\w* does most of the job, but I have no idea how to add that tester should not be matched. A working pattern should give 3 matches on the string.
Does this work for your purposes?
test(?!er).*
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".