Remove Words from a Text

1

I have a string with data and what I want to do is take out desired words that have specific characters and then add each word to an array [] with data taken from the text

string datos = "Hay datos <Etiqueta>que [re]quiero <Etiqueta>Hola que tal";

In this string I want to extract the words that are after . and until the space is found

In this case, the result I want to obtain would be:

string[] Datos = new string[2];

        Datos[0] = "que";
        Datos[1] = "Hola";
    
asked by Diego 28.04.2017 в 23:36
source

2 answers

2

You can try to use Regex I'll give you a way to use it that I think is what search:

using System;
using System.Text.RegularExpressions;

.

       string pattern = @"<Etiqueta>(?<despues>\w+)";

        string input = @"<Hay datos <Etiqueta>que [re]quiero <Etiqueta>Hola que tal";
        MatchCollection matches = Regex.Matches(input, pattern);

        for (int i = 0; i < matches.Count; i++)
        {
           Console.WriteLine("despues de <Etiqueta>:" + matches[i].Groups["despues"].ToString());
        }

Exit:

despues de <Etiqueta>:que
despues de <Etiqueta>:Hola

System.Text.RegularExpressions provide access to the .NET regular expression engine

    
answered by 29.04.2017 / 00:17
source
2

Use the following regular expression:

<Etiqueta>([a-záéíñóúü]+)

Code:

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;


public class Test
{
    public static void Main()
    {
        string pattern = @"<Etiqueta>([a-záéíñóúü]+)";
        string input = @"Hay datos <Etiqueta>que [re]quiero <Etiqueta>Hola que tal";
        RegexOptions options = RegexOptions.IgnoreCase;
        List<string> list = new List<string>();

        foreach (Match m in Regex.Matches(input, pattern, options))
        {
            list.Add(m.Groups[1].Value);
        }

        string[] Datos = list.ToArray();
        Console.WriteLine(string.Join("\n", Datos));
    }
}

Exit:

que
Hola

See demo online.

The extraction is done with regular expressions (see

answered by 29.04.2017 в 00:11