How capture nested groups? [duplicate] - c#

This question already has answers here:
What are regular expression Balancing Groups?
(2 answers)
Closed 8 years ago.
I have a target string like this: (however nesting can be very deep in practice)
{hi {how {are {you}}}}
Desired result would be:
Groups:
hi how are you
I can't find anything in c# regex to do nested capturing like that. Is it possible at all?
EDIT:
I think I simplified my example too much which obscures the answers.
I need to capture in a recursive sort of way because I need the content inside the brackets:
{test[{test2[content]}]}
where the desired result would be:
{test2[content]} and content

{([^{}]+)
Try this.See demo.grab the captures.
http://regex101.com/r/oE6jJ1/35

I could think of this:
string balh = "{hi {how {are {you}}}}";
string[] foo = balh.Split(new char[] { '{', '}' }, StringSplitOptions.RemoveEmptyEntries);
string output = string.Join(" ", foo);
The groups will be in foo array.
EDIT:
I think you are looking for more elaborate inputs. I got the question wrong :(

Related

Can't get 2 different Substrings from a String C# [duplicate]

This question already has answers here:
split a string on newlines in .NET
(17 answers)
Closed 4 years ago.
I have a task.
I should extract two substrings from a string.
The lenght of the String will be different each time, so the method should be generic.
Suppose I have the following String:
/*Description:\r\n*RANGE:\r\n*HIGH\r\n*LOW\r\n*/
I need to get the substring1= HIGH and substring2= LOW.
The substring1 and substring2 will be all the time between \r\n, but they values will be different.
I would be very grateful if anybody helps me. It can be a pseudocode, anything.
Thanks in advance.
UPDATE1: I'm searching first for "RANGE:\r\n*" and get the index of the character * and the index og character "H". But next don't know how to get the whole substring.
If the pattern you've provided is similar to what you'd expect all the time, a stupid simple approach would be:
public static string[] GetParts(string input)
{
string[] parts= input.Split("\r\n", StringSplitOptions.RemoveEmptyEntries);
return parts.TakeLast(2).Select(item=>item.Replace("*", string.Empty)).ToArray();
}
Note: This is not a production quality code.

C# Regular Expression - find groups in text with a separator [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 6 years ago.
I have the following text
"a|mother" "b|father"
I want to find via Regex, groups of text that starts with '"' and ends with '"' and separate with '|' without spaces. Meaning the results would be:
"a|mother"
"b|father"
I try to use other posts to solve my question but still with no luck how can I find the |? and how can I find my pattern without spaces?
Something like this:
String source = "\"a|mother\" \"b|father\"";
var result = Regex
.Matches(source, "\"[^\"]*[^ ]\\|[^ ][^\"]*\"")
.OfType<Match>();
Console.Write(String.Join(Environment.NewLine, result));
Output is
"a|mother"
"b|father"

parse selected text to string ignoring chars [duplicate]

This question already has answers here:
Regex to get NUMBER only from String
(3 answers)
Closed 7 years ago.
Im taking a selected.text which can equal 123t, and id like to just take the 123 and insert it into a string. the selected.text can also simply equal 54, or 256, but occasionally it will have a letter. Im unsure of which what to select or parse to remove the letter. i tried to do something convoluted like this as i saw similar questions on SE,
string cat;
int dog;
cat = txtFrame.Text;
dog = int.Parse(cat.Substring(cat.IndexOf("")));
Frame = dog.ToString();
Frame is the string i would like it to end up in.
This is probably best done with Regex
string test = "123t";
Match m = Regex.Match(test, #"\d+");
Console.WriteLine(m.Value);
If an eventual sign is possible in your string then you could use an expanded form of the pattern
Regex.Match(test, #"[-+]?\d+")
From the following post ...
How do you remove all the alphabetic characters from a string?
Regex.Replace(s, "[^0-9.+-]", "")
#Steve's answer is better!
Here's a good resource for RegEx ... http://www.regular-expressions.info/

Splitting these string without clobbering split param's? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to split a string while preserving line endings?
How do I split a string by strings and include the delimiters using .NET?
I'm splitting text into sentences. mystring.Split('.','!', '?') returns the sentences without the ./!/? on them. I need to have it return a sentence with the split param on the end? How does that go? Thanks
public static string[] GetSentences(string text)
{
return text.Split('.', '!', '?');
}
I can think of one way to do it, by combining two separate arrays, but I think it looks awful so I thought I'd ask you professionals for a "proper" way :D
Edit - never mind close its a duplicate. I found the other threads, sorry
Right, string.Split() isn't the right tool here.
Either simply loop through it (string.IndexOf())
or use a RegEx: ([^\.!?]+[\.!?])*
I'm not 100% sure about the escaping.

How to make a "minimal match" Regex search in C#? [duplicate]

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".

Categories