0

I have an xml file:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="name">word1</string>
<string name="namee">word2</string>
<string name="nameee">word3</string>

</resources>

I want to find every word between > <. Therefore word1, word2 and word3. I have written some code but that regex expression find only the first word (word1).

    private void button1_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            StreamReader sr = new StreamReader(openFileDialog1.FileName);
            string s = sr.ReadToEnd();
            richTextBox1.Text = s;
        }

        string txt = richTextBox1.Text;
        string foundWords = Regex.Match(txt, @"\>(\w+)\<").Groups[1].Value;
        richTextBox1.Text = foundWords;

    }

I want to find every word between > < and display it in a rich textbox.

svick
  • 236,525
  • 50
  • 385
  • 514
orglce
  • 513
  • 2
  • 7
  • 19

2 Answers2

2

You should probably look at parsing XML with something else baked into .NET.

Having said that:

You're only getting the first one because you're using Match. (matches a single value) Try using Matches instead (returns a collection of Match values that you can iterate through).

Try:

list<String> foundWords = new List<String>();
var foundMatches = regex.matches(txt, @"\>(\w+)<");
foreach(match m in foundMatches)
{
     foundWords.add(m.Groups[1].Value);
}
//do something with list of foundWords
Kyle Pittman
  • 2,858
  • 1
  • 30
  • 38
  • This is an awful suggestion to parse XML with regular expressions - if OP wanted to use regular expression for something it does not mean it suitable for task. – Alexei Levenkov Nov 05 '13 at 20:26
1

use method Regex.Matches() to capture MatchCollection

private void button1_Click(object sender, EventArgs e)
{
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        StreamReader sr = new StreamReader(openFileDialog1.FileName);
        string s = sr.ReadToEnd();
        richTextBox1.Text = s;
    }

    string txt = richTextBox1.Text;
    var foundWords = Regex.Matches(txt, @"(?<=>)(\w+?)(?=<)");
    richTextBox1.Text = string.Join("\n", foundWords.Cast<Match>().Select(x=>x.Value).ToArray());
}
burning_LEGION
  • 13,246
  • 8
  • 40
  • 52