Regex | Catch separate group [closed]

-1

This tag repeats changing the content and would like to know if you can get each content separately

    TextBox TXTTESTE = new TextBox();
    TextBox TXTTESTEDOIS = new TextBox();
    TextBox TXTTESTETRES = new TextBox();
    ListBox LBTESTE = new ListBox();
    MatchCollection TESTE = Regex.Matches(html, @"(<[^>]+>Alíquota<[^>]+><[^>]+>\d+\,\d+<[^>]+>)", RegexOptions.Multiline);
    foreach (Match grupo in TESTE)
    {
         string strteste = grupo.Groups[0].Value;
         string strdoisteste = grupo.Groups[1].Value;
         string strtresteste = grupo.Groups[2].Value;
         LBTESTE.Items.Add(strteste);
         LBTESTE.Items.Add(strdoisteste);
         LBTESTE.Items.Add(strtresteste);
    }
    
asked by anonymous 15.04.2014 / 22:29

2 answers

2

Do you want to redeem only the right values? In this case, you should create a group only in value:

<[^>]+>Alíquota<[^>]+><[^>]+>(\d+\,\d+)<[^>]+>

Note that I am putting \d+\,\d+ in parentheses to create a catch group.

Note also that I removed the outer parentheses.

Then you can use foreach to iterate over matches, and get from each one the catch group we defined earlier:

foreach (Match grupo in TESTE)
{
    LBTESTE.Items.Add(grupo.Groups[1].Value);
}
  • Groups[0] : is the capture group that is equivalent to the entire regex. In the case of the HTML indicated by you they are:

    • <label>Alíquota</label><span class="linha">18,00</span>

    • <label>Alíquota</label><span class="linha">50,00</span>

  • Groups[1] : is the catch group I defined, using parentheses. In the case of the HTML indicated by you they are:

    • 18,00

    • 50,00

  • Other groups can exist, numbered sequentially, for each open / close parentheses that are found in the regular expression.

15.04.2014 / 22:54
1

Yes, it does. You are not using the concept of groups correctly. A group is defined as follows:

(?<nome>.*?)

To access:

foreach (Match grupo in TESTE) {
    grupo.Groups["nome"];
}
    
15.04.2014 / 22:36