Jump to content

Substituir Grupos encontrados (REGEX)


Recommended Posts

Posted

Boas estou a tentar criar uma função que faça a substituição de strings encontradas por Regex.

O que está a acontecer é que a função está a retornar apenas um grupo e a repeti-lo.

Aqui tenho a função.

    string encontrar = @"t[a-z]{4}\d{3}";

            Match resultado = Regex.Match(sql, encontrar);

            if(resultado.Success)
            {
                MatchCollection grupo = Regex.Matches(sql, encontrar);

                for(int i = 0; i < grupo.Count; i++)
                {
                    sql = Regex.Replace(sql, encontrar, "t" + grupo[i] + "100");
                }
            }
            return sql;

Alguma ajuda, Obrigado

You can't do it, kid. But don't worry, my boy. You're not the only one. No one else can do it.

Posted

Já resolvi, desta forma, podem fechar o tópico.
 

            string nsql = null;

            string encontrar = @"t[a-zA-Z]{4}\d{3}";

            Match resultado = Regex.Match(sql, encontrar);

            MatchCollection grupo = Regex.Matches(sql, encontrar);

            foreach(Match m in grupo)
            {
               nsql = Regex.Replace(sql, encontrar, "t{$0}" + empresa, RegexOptions.Singleline);
            }

            nsql = Regex.Replace(nsql, @"{", String.Empty);
            nsql = Regex.Replace(nsql, @"}", String.Empty);

            return nsql;

You can't do it, kid. But don't worry, my boy. You're not the only one. No one else can do it.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.