|
Il presente articolo è stato tradotto automaticamente. Passare il puntatore sulle frasi nell'articolo per visualizzare il testo originale. Ulteriori informazioni.
|
Traduzione
Originale
|
Classe Regex
Spazio dei nomi: System.Text.RegularExpressions
Assembly: System (in System.dll)
Il tipo Regex espone i seguenti membri.
| Nome | Descrizione | |
|---|---|---|
![]() ![]() ![]() ![]() | Regex() | |
![]() ![]() ![]() ![]() | Regex(String) | |
![]() | Regex(SerializationInfo, StreamingContext) | |
![]() ![]() ![]() ![]() | Regex(String, RegexOptions) | |
![]() ![]() | Regex(String, RegexOptions, TimeSpan) |
| Nome | Descrizione | |
|---|---|---|
![]() ![]() | CacheSize | |
![]() ![]() | MatchTimeout | |
![]() ![]() ![]() ![]() | Options | |
![]() ![]() ![]() ![]() | RightToLeft |
| Nome | Descrizione | |
|---|---|---|
![]() ![]() | CompileToAssembly(RegexCompilationInfo[], AssemblyName) | |
![]() ![]() | CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[]) | |
![]() ![]() | CompileToAssembly(RegexCompilationInfo[], AssemblyName, CustomAttributeBuilder[], String) | |
![]() ![]() ![]() ![]() | Equals(Object) | |
![]() ![]() ![]() ![]() ![]() | Escape | |
![]() ![]() ![]() ![]() | Finalize | |
![]() ![]() ![]() ![]() | GetGroupNames | |
![]() ![]() ![]() ![]() | GetGroupNumbers | |
![]() ![]() ![]() ![]() | GetHashCode | |
![]() ![]() ![]() ![]() | GetType | |
![]() ![]() ![]() ![]() | GroupNameFromNumber | |
![]() ![]() ![]() ![]() | GroupNumberFromName | |
![]() | InitializeReferences | Infrastruttura. |
![]() ![]() ![]() ![]() | IsMatch(String) | |
![]() ![]() ![]() ![]() | IsMatch(String, Int32) | |
![]() ![]() ![]() ![]() ![]() | IsMatch(String, String) | |
![]() ![]() ![]() ![]() ![]() | IsMatch(String, String, RegexOptions) | |
![]() ![]() ![]() | IsMatch(String, String, RegexOptions, TimeSpan) | |
![]() ![]() ![]() ![]() | Match(String) | |
![]() ![]() ![]() ![]() | Match(String, Int32) | |
![]() ![]() ![]() ![]() ![]() | Match(String, String) | |
![]() ![]() ![]() ![]() | Match(String, Int32, Int32) | |
![]() ![]() ![]() ![]() ![]() | Match(String, String, RegexOptions) | |
![]() ![]() ![]() | Match(String, String, RegexOptions, TimeSpan) | |
![]() ![]() ![]() ![]() | Matches(String) | |
![]() ![]() ![]() ![]() | Matches(String, Int32) | |
![]() ![]() ![]() ![]() ![]() | Matches(String, String) | |
![]() ![]() ![]() ![]() ![]() | Matches(String, String, RegexOptions) | |
![]() ![]() ![]() | Matches(String, String, RegexOptions, TimeSpan) | |
![]() ![]() ![]() ![]() | MemberwiseClone | |
![]() ![]() ![]() ![]() | Replace(String, String) | |
![]() ![]() ![]() ![]() | Replace(String, MatchEvaluator) | |
![]() ![]() ![]() ![]() | Replace(String, String, Int32) | |
![]() ![]() ![]() ![]() ![]() | Replace(String, String, String) | |
![]() ![]() ![]() ![]() ![]() | Replace(String, String, MatchEvaluator) | |
![]() ![]() ![]() ![]() | Replace(String, MatchEvaluator, Int32) | |
![]() ![]() ![]() ![]() | Replace(String, String, Int32, Int32) | |
![]() ![]() ![]() ![]() ![]() | Replace(String, String, String, RegexOptions) | |
![]() ![]() ![]() ![]() ![]() | Replace(String, String, MatchEvaluator, RegexOptions) | |
![]() ![]() ![]() ![]() | Replace(String, MatchEvaluator, Int32, Int32) | |
![]() ![]() ![]() | Replace(String, String, String, RegexOptions, TimeSpan) | |
![]() ![]() ![]() | Replace(String, String, MatchEvaluator, RegexOptions, TimeSpan) | |
![]() ![]() ![]() ![]() | Split(String) | |
![]() ![]() ![]() ![]() | Split(String, Int32) | |
![]() ![]() ![]() ![]() ![]() | Split(String, String) | |
![]() ![]() ![]() ![]() | Split(String, Int32, Int32) | |
![]() ![]() ![]() ![]() ![]() | Split(String, String, RegexOptions) | |
![]() ![]() ![]() | Split(String, String, RegexOptions, TimeSpan) | |
![]() ![]() ![]() ![]() | ToString | |
![]() ![]() ![]() ![]() ![]() | Unescape | |
![]() ![]() | UseOptionC | Infrastruttura. |
![]() ![]() | UseOptionR | Infrastruttura. |
![]() ![]() | ValidateMatchTimeout | Infrastruttura. |
| Nome | Descrizione | |
|---|---|---|
![]() ![]() | capnames | Infrastruttura. |
![]() ![]() | caps | Infrastruttura. |
![]() ![]() | capsize | Infrastruttura. |
![]() ![]() | capslist | Infrastruttura. |
![]() | factory | Infrastruttura. |
![]() ![]() ![]() | InfiniteMatchTimeout | |
![]() | internalMatchTimeout | Infrastruttura. |
![]() ![]() | pattern | Infrastruttura. |
![]() ![]() | roptions | Infrastruttura. |
| Nome | Descrizione | |
|---|---|---|
![]() ![]() | ISerializable.GetObjectData | Infrastruttura. |
Nota |
|---|
Regex VS. metodi di stringa
Statico VS. i metodi di istanza
Creando un'istanza di un oggetto Regex che rappresenta l'espressione regolare. A tal proposito, occorre passare il modello di espressione regolare a un costruttore Regex. Un oggetto Regex non è modificabile; quando si crea un'istanza di un oggetto Regex con un'espressione regolare, l'espressione regolare di quel oggetto non può essere modificata. Fornendo sia l'espressione regolare che il testo da cercare a un metodo static (Shared in Visual Basic) Regex. In questo modo, è possibile utilizzare un'espressione regolare senza creare esplicitamente un oggetto Regex.
Importante |
|---|
Quando si utilizzano chiamate a metodi statici con un gran numero di espressioni regolari. Per impostazione predefinita, il motore delle espressioni regolari memorizza nella cache le 15 espressioni regolari statiche utilizzate più di recente. Se l'applicazione in uso utilizza più di 15 espressioni regolari statiche, alcune espressioni regolari devono essere compilate di nuovo. Per impedire questo ricompilazione, è possibile aumentare la proprietà Regex.CacheSize. Quando viene creata un'istanza Regex nuovo oggetto con le espressioni regolari che erano state compilate. Ad esempio, il codice seguente viene definita un'espressione regolare per individuare le parole duplicate in un flusso di testo. Sebbene nell'esempio venga utilizzata una singola espressione regolare, viene creata l'istanza di un nuovo oggetto Regex per elaborare ciascuna riga di testo. Il risultato è la ricompilazione dell'espressione regolare con ciascuna iterazione del ciclo. StreamReader sr = new StreamReader(filename); string input; string pattern = @"\b(\w+)\s\1\b"; while (sr.Peek() >= 0) { input = sr.ReadLine(); Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase); MatchCollection matches = rgx.Matches(input); if (matches.Count > 0) { Console.WriteLine("{0} ({1} matches):", input, matches.Count); foreach (Match match in matches) Console.WriteLine(" " + match.Value); } } sr.Close();
Per impedire la ricompilazione, è necessario creare un'istanza di un singolo oggetto Regex accessibile al codice che lo richiede, come illustrato nell'esempio di. StreamReader sr = new StreamReader(filename); string input; string pattern = @"\b(\w+)\s\1\b"; Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase); while (sr.Peek() >= 0) { input = sr.ReadLine(); MatchCollection matches = rgx.Matches(input); if (matches.Count > 0) { Console.WriteLine("{0} ({1} matches):", input, matches.Count); foreach (Match match in matches) Console.WriteLine(" " + match.Value); } } sr.Close();
Esecuzione di operazioni relative a espressioni regolari
Convalida di una corrispondenza. Il metodo IsMatch consente di determinare se è presente una corrispondenza. Recupero di una singola corrispondenza. Il metodo Match consente di recuperare un oggetto Match che rappresenta la prima corrispondenza in una stringa o in una parte di stringa. Le corrispondenze successive possono essere recuperate chiamando il metodo Match.NextMatch. Recupero di tutte le corrispondenze. Il metodo Matches viene chiamato per recuperare un oggetto System.Text.RegularExpressions.MatchCollection che rappresenta tutte le corrispondenze trovate in una stringa o in una parte di stringa. Sostituzione di testo corrispondente. Il metodo Replace può essere chiamato per sostituire testo corrispondente. Il testo di sostituzione può essere definito anche da un'espressione regolare. Inoltre, alcuni dei metodi Replace includono un parametro MatchEvaluator che consente di definire a livello di codice il testo di sostituzione. Creazione di una matrice di stringhe che è formata da parti di una stringa di input. Il metodo Split viene chiamato per dividere una stringa di input in corrispondenza di posizioni che sono definite dall'espressione regolare.
Il metodo Escape effettua l'escape di caratteri che possono essere interpretati come operatori di espressioni regolari in un'espressione regolare o in una stringa di input. Il metodo Unescape consente di rimuovere questi caratteri di escape. Il metodo CompileToAssembly crea un assembly che contiene espressioni regolari predefinite. .NET Framework contiene esempi di questi assembly per usi specifici nello spazio dei nomi System.Web.RegularExpressions.
Definizione del valore di timeout
L'utilizzo dell'applicazione il valore di timeout, se presente. Può trattarsi di qualsiasi valore di timeout applicato al dominio applicazione in cui l'oggetto Regex viene creata un'istanza o la chiamata al metodo statico viene eseguita. È possibile impostare sull'intera il valore di timeout chiamando il metodo AppDomain.SetData per assegnare la rappresentazione di stringa di un valore TimeSpan alla proprietà “di REGEX_DEFAULT_MATCH_TIMEOUT„. Utilizzando il valore InfiniteMatchTimeout, se non esplicitamente il valore di timeout è stato impostato.
Importante |
|---|
\b | |
(?<word>\w+) | |
\s+ | |
(\k<word>) | |
\b |
using System; using System.Text.RegularExpressions; public class Test { public static void Main () { // Define a regular expression for repeated words. Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b", RegexOptions.Compiled | RegexOptions.IgnoreCase); // Define a test string. string text = "The the quick brown fox fox jumped over the lazy dog dog."; // Find matches. MatchCollection matches = rx.Matches(text); // Report the number of matches found. Console.WriteLine("{0} matches found in:\n {1}", matches.Count, text); // Report on each match. foreach (Match match in matches) { GroupCollection groups = match.Groups; Console.WriteLine("'{0}' repeated at positions {1} and {2}", groups["word"].Value, groups[0].Index, groups[1].Index); } } } // The example produces the following output to the console: // 3 matches found in: // The the quick brown fox fox jumped over the lazy dog dog. // 'The' repeated at positions 0 and 4 // 'fox' repeated at positions 20 and 25 // 'dog' repeated at positions 50 and 54
^ | |
\s* | |
[\+-]? | |
\s? | |
\$? | |
\s? | |
\d* | |
\.? | |
\d{2}? | |
(\d*\.? \d{2}?){1} | |
$ |
using System; using System.Globalization; using System.Text.RegularExpressions; public class Example { public static void Main() { // Get the current NumberFormatInfo object to build the regular // expression pattern dynamically. NumberFormatInfo nfi = NumberFormatInfo.CurrentInfo; // Define the regular expression pattern. string pattern; pattern = @"^\s*["; // Get the positive and negative sign symbols. pattern += Regex.Escape(nfi.PositiveSign + nfi.NegativeSign) + @"]?\s?"; // Get the currency symbol. pattern += Regex.Escape(nfi.CurrencySymbol) + @"?\s?"; // Add integral digits to the pattern. pattern += @"(\d*"; // Add the decimal separator. pattern += Regex.Escape(nfi.CurrencyDecimalSeparator) + "?"; // Add the fractional digits. pattern += @"\d{"; // Determine the number of fractional digits in currency values. pattern += nfi.CurrencyDecimalDigits.ToString() + "}?){1}$"; Regex rgx = new Regex(pattern); // Define some test strings. string[] tests = { "-42", "19.99", "0.001", "100 USD", ".34", "0.34", "1,052.21", "$10.62", "+1.43", "-$0.23" }; // Check each test string against the regular expression. foreach (string test in tests) { if (rgx.IsMatch(test)) Console.WriteLine("{0} is a currency value.", test); else Console.WriteLine("{0} is not a currency value.", test); } } } // The example displays the following output: // -42 is a currency value. // 19.99 is a currency value. // 0.001 is not a currency value. // 100 USD is not a currency value. // .34 is a currency value. // 0.34 is a currency value. // 1,052.21 is not a currency value. // $10.62 is a currency value. // +1.43 is a currency value. // -$0.23 is a currency value.
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (ruoli di base del server non supportati), Windows Server 2008 R2 (ruoli di base del server supportati con SP1 o versione successiva, Itanium non supportato)
.NET Framework non supporta tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema di .NET Framework.
System.Text.RegularExpressions.Regex
System.Web.RegularExpressions.AspCodeRegex
System.Web.RegularExpressions.AspEncodedExprRegex
System.Web.RegularExpressions.AspExprRegex
System.Web.RegularExpressions.CommentRegex
System.Web.RegularExpressions.DatabindExprRegex
System.Web.RegularExpressions.DataBindRegex
System.Web.RegularExpressions.DirectiveRegex
System.Web.RegularExpressions.EndTagRegex
System.Web.RegularExpressions.GTRegex
System.Web.RegularExpressions.IncludeRegex
System.Web.RegularExpressions.LTRegex
System.Web.RegularExpressions.RunatServerRegex
System.Web.RegularExpressions.ServerTagsRegex
System.Web.RegularExpressions.SimpleDirectiveRegex
System.Web.RegularExpressions.TagRegex
System.Web.RegularExpressions.TagRegex35
System.Web.RegularExpressions.TextRegex


