Sunteți pe pagina 1din 3

EXPRESIONES REGULARES

NAMESPACE : System.Text.RegularExpressions

How to : VALIDAR UN TEXTO CON UNA EXPRESION REGULAR


System.Text.RegularExpressions.Regex.IsMatch(Expresion Regular, Cadena);

Caracteres Especiales

^ Comienzo de Cadena
\d Solo digitos
\s Solo espacios en blanco, tabs
\w Solo cadenas
{5} Longitud 5
$ Fin de Cadena

How to : SETEAR OPCIONES PARA BUSQUEDA DE EXPRESIONES REGULARES


RegexOptions
None
IgnoreCase
Multiline
ExplicitCapture
Compiled
Singleline
IgnorePattern-Whitespace
RightToLeft

Regex.IsMatch(s, "^def$", RegexOptions.Multiline)

match.Success : Flag que indica si se encontro una coincidencia o no.

/*Groups collection on Match objects starts at 1.*/

How to : OBTENER LAS COINCIDENCIAS DE UNA EXPRESION REGULAR EN UN


TEXTO
// C#
string input = "Company Name: Contoso, Inc.";
Match m = Regex.Match(input, @"Company Name: (.*$)");
Console.WriteLine(m.Groups[1]);

How to : REEMPLAZAR SUBCADENAS USANDO EXPRESIONES REGULARES


String MDYToDMY(String input)
{
return Regex.Replace(input,
"\\b(?<month>\\d{1,2})/(?<day>\\d{1,2})/(?<year>\\d{2,4})\\b",
"${day}-${month}-${year}");
}

ENCODING/DECODING
NAMESPACE : System.Text.Encoding.GetEncoding

HOW TO : OBTENER LOS BYTES DE UNA CADENA CON UNA CODIFICACION


ESPECIFICA.

Encoding.GetBytes

How to : OBTENER LAS CODIFICACIONES DISPONIBLES

EncodingInfo[] ei = Encoding.GetEncodings();

How to : ESPECIFICAR CODIFICACION CUANDO SE LEE O ESCRIBE UN ARCHIVO

// ESCRITURA
StreamWriter swUtf7 = new StreamWriter("utf7.txt", false, Encoding.UTF7);
swUtf7.WriteLine("Hello, World!");
swUtf7.Close();

// LECTURA
StreamReader sr = new StreamReader(fn, Encoding.UTF7);
Console.WriteLine(sr.ReadToEnd());
sr.Close();

SOLUCION EJERCICIOS

/*
Practice 1
Write a console application that reads your C:\boot.ini file and displays
just the timeout.

Practice 4
Write a console application that reads the %windir%\Windows-
Update.log file, changes the date format to mm-dd-yy, and writes the output
to a
second file.

Practice 5
Write a console application with a method that reads the
%windir%\WindowsUpdate.log file and writes the output to a second file using
an encoding type provided in a parameter. Compare the file sizes of each
encoding
type.
*/

// 1.
/*
StreamReader streamReader = new
StreamReader(@"C:\Users\Carlos\Documents\Visual Studio
2005\Projects\MCTS_Testing\MCTS_Testing\Capitulo3\ArchivoBusqueda.txt");
String archivoContenido = streamReader.ReadToEnd();
Match match = Regex.Match(archivoContenido, @"%([A-Za-z0-9\-]+)%"); //
([A-Za-z0-9\-]+) : Alfanumerico
if (match.Success)
{
Console.WriteLine(match.Groups[1]);
}
else
{
Console.WriteLine("Failure");
}
*/

//4.
// StreamReader streamReader = new
StreamReader(@"C:\Users\Carlos\Documents\Visual Studio
2005\Projects\MCTS_Testing\MCTS_Testing\Capitulo3\ArchivoBusqueda.txt");
//String archivoContenido = streamReader.ReadToEnd();
//String nuevaCadena = Regex.Replace(archivoContenido, @"%([A-Za-
z0-9\-]+)%", "tomcat"); // ([A-Za-z0-9\-]+) : Alfanumerico
//Console.WriteLine(nuevaCadena);

//5.
//StreamReader streamReader = new
StreamReader(@"C:\Users\Carlos\Documents\Visual Studio
2005\Projects\MCTS_Testing\MCTS_Testing\Capitulo3\ArchivoBusqueda.txt");
//String archivoContenido = streamReader.ReadToEnd();
//StreamWriter streamWriter = new
StreamWriter(@"C:\Users\Carlos\Documents\Visual Studio
2005\Projects\MCTS_Testing\MCTS_Testing\Capitulo3\ArchivoBusqueda2.txt",
false,Encoding.BigEndianUnicode);
//streamWriter.Write(archivoContenido);

//streamReader.Close();
//streamWriter.Close();

S-ar putea să vă placă și