Search for "pattern" in string C #

0

Is there a method to find a pattern with specific characteristics within a string? I need to look at a string and if any of the parts contains a (xxxxxxx) delete it, the problem is that what is inside the parentheses can be variable but will always have the same length, that is, if what is inside the parenthesis has 7 characters I want to eliminate but if not, keep it as is.

    
asked by Edulon 16.11.2017 в 11:12
source

1 answer

2

It is best to use a Regular Expression using the Regex.Replace .

In your case, this code would do what you want:

string cadena = "Esto es una cadena(as12eth) de (asfghjk)prueba";
Regex rgx = new Regex(@"\(.{7}\)");
cadena = rgx.Replace(cadena, "");
//resultado: cadena="Esto es una cadena de prueba"

Explanation of the regex:

\( - busca el carácter '('
.  - cualquier carácter
{7}- que se repita 7 veces
\) - carácter ')'
    
answered by 16.11.2017 / 11:28
source