Le regular expression permettono di ricercare, tramite 'pattern' più o meno complessi, porzioni stringhe in un testo.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjSj3UjdoQVhADW7viPCeZcOjkBJg_DaUBmKk2HX9nBkHNVlsYjRIItCCZvrBt85Ipd-R4d8jlkcq2QxR2lJ5Jw7jv2xsRVUX8ZmuMeiN8Ua2gaiECL-PhcVo9PxVcd-R9ykjmyPHTDDyi/s320/Immagine1.bmp)
Creo la regular expression con questa utility http://www.regexbuddy.com/. Ce ne sono anche di gratuite (ad esempio http://www.ultrapico.com/Expresso.htm).
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhY07hfjWNvbM_cRyCzTh8UmrLxw1iF6HcJIS_UyRt-G6Ss4vAcO0ut7mGtKpDKMds5ZOLmF_4XnXZZF_pjx3szw-aGc9lG_F6R76eYPvLa05dcEVHXaqPl-b-kvgW5OsgzwC7YT9t5yUop/s400/Immagine2.bmp)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjh00Ka7u1AdsGWorUIE4bx61SwSN6nqIhelgpnHzHQ4YViv2KpbLDW3NOnZy73gOyiWhT8R4MkXRrGRNkRLXUfVT8gaqvupavmtuKNDNI3zS7LUKxEJezoOnJgu4qJSnqINPAt4dVw2dp/s400/Immagine3.JPG)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWJBiz4N-PAdIrXjR-M9eqIKf_l1mLDKz8vPWmMDHwgdzT7vazoleoaeve7X-zLSxmsF3NDzFEdQ2X1F3mTR880Rk55CxOzydOdWJAC0QYsFfZOj7U6xkJ1SV54C4wpR3WN4XbXdyCRMEK/s400/Immagine4.JPG)
Testo:
12,345
1234,45
1234.45
1234567890
+12,34
-12,34
+1234
-1234
Attention: 1 2 3 test
The number 12345678901234567890 is quite long
1+1=2
troveremo:1234567890
In c# la classe che si occupa di questo è System.Text.RegularExpressions.Regex.
Creiamo una semplice form con una textbox ed un button per testare la regular expression.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjSj3UjdoQVhADW7viPCeZcOjkBJg_DaUBmKk2HX9nBkHNVlsYjRIItCCZvrBt85Ipd-R4d8jlkcq2QxR2lJ5Jw7jv2xsRVUX8ZmuMeiN8Ua2gaiECL-PhcVo9PxVcd-R9ykjmyPHTDDyi/s320/Immagine1.bmp)
Creo la regular expression con questa utility http://www.regexbuddy.com/. Ce ne sono anche di gratuite (ad esempio http://www.ultrapico.com/Expresso.htm).
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhY07hfjWNvbM_cRyCzTh8UmrLxw1iF6HcJIS_UyRt-G6Ss4vAcO0ut7mGtKpDKMds5ZOLmF_4XnXZZF_pjx3szw-aGc9lG_F6R76eYPvLa05dcEVHXaqPl-b-kvgW5OsgzwC7YT9t5yUop/s400/Immagine2.bmp)
Ad esempio vogliamo estrarre tutte le parole racchiuse tra parentesi quadre.
Invece di creare codice utilizzando ad esempio i classici substring, indexof ecc. abbiamo lo stesso risultato con poche righe di codice.
In un testo ad esempio vogliamo che non ci siano due parole uguali ripetute:
try
{
Regex regexObj = new Regex(@"\b(\w+)\s+\1\b");
Match matchResults = regexObj.Match(subjectString);
while (matchResults.Success)
{
// matched text: matchResults.Value
// match start: matchResults.Index
// match length: matchResults.Length
matchResults = matchResults.NextMatch();
}
}
catch(ArgumentException ex)
{
// Syntax error in the regular expression
}
Testo: 'This word is a repeated repeated word. A pluto pluto'
troveremo: repeated repeatedpluto pluto
Se è un intero ancorato:
troveremo: repeated repeatedpluto pluto
Se è un intero ancorato:
try {
Regex regexObj = new Regex(@"^\d+$");
Match matchResults = regexObj.Match(subjectString);
while (matchResults.Success) {
// matched text: match Results.Value
// match start: matchResults.Index
// match length: matchResults.Length
matchResults = matchResults.NextMatch();
}
} catch (ArgumentException ex)
{
// Syntax error in the regular expression
}
Testo:
12,345
1234,45
1234.45
1234567890
+12,34
-12,34
+1234
-1234
Attention: 1 2 3 test
The number 12345678901234567890 is quite long
1+1=2
troveremo:1234567890
Nessun commento:
Posta un commento