Regex | Take group split

Asked

Viewed 1,016 times

-1

This tag repeats by changing the content and I would like to know if there is a way to 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);
    }
  • Edit and put the example tag.

  • it returns "10.00", "20.00", "30.00" "10.00" , "20.00" , "30.00"

  • There is a software to run regex . Net that I use a lot to test regexes quickly when needed: Rad Software Regular Expression Designer

  • I’ll improve the question, what’s inside html ?

  • You have this in HTML: <td><label>Aliquot</label><span class="line">18,00</span></td></tr><td><label>Aliquot</label><span class="line">50,00</span></td></tr>

2 answers

2


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

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

Note that I’m putting \d+\,\d+ between parentheses, to create a capture group.

Note also that I removed the most external parentheses.

Then it is possible to use the foreach to iterate over the Matches, and get from each, the capture 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 all regex. In the case of the HTML indicated by you are they:

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

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

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

    • 18,00

    • 50,00

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

  • when I capture the group have how I set the item1 / item2 / item3 in order ? can I use them when I want? without being in the order they are in?

  • Deslculpa, I don’t understand... could explain better?

  • You can add all the values in an intermediate list, before adding in the list-box, so you can do whatever you want.

  • it is possible to add an index in the group and when I speak group. Groups[1/2/3]. Value it calls the value Item1 / Item2 / Item3 if yes , as?

  • In fact, you can do this for the Smes, which in your case is in the variable TESTE... thus: TESTE[n].Groups[1].Value, where n is the index you will use: 0, 1, 2, 3...

  • would also like to know how the Nextmatch method works()

  • This method is used when you have a Match object, and want to catch the next... use the method Regex.Matches is the same as using Regex.Match and then charm NextMatch until there is no match left.

  • Miguel Voce told me that I can call TESTE[]. Groups[]. Value but it only defines the value of regex

  • So I guess I really don’t understand your question.

  • I managed to tidy I removed the items from the list and used the ones I needed so thank you very much, I was not thinking very clearly

Show 5 more comments

1

Yes. You are not using the group concept correctly. A group is defined as follows:

(?<nome>.*?)

To access:

foreach (Match grupo in TESTE) {
    grupo.Groups["nome"];
}
  • and after I defined the group as I divide it?

  • Placing the expression in parentheses, right after the >.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.