2010-03-13 10 views
64

È possibile eseguire una corrispondenza senza distinzione tra maiuscole e minuscole in C# utilizzando la classe Regex senza impostare il flag RegexOptions.IgnoreCase?Regex insensibile alle maiuscole senza utilizzare l'enumerazione RegexOptions

Quello che vorrei essere in grado di fare è all'interno della regex stessa definire se voglio o meno che l'operazione di corrispondenza venga eseguita in un modo non sensibile alla distinzione tra maiuscole e minuscole.

Vorrei che questo regex, taylor, per abbinare i seguenti valori:

  • Taylor
  • taylor
  • Taylor

risposta

93

MSDN Documentation

(?i)taylor partite tutti gli input I s specificato senza dover impostare il flag RegexOptions.IgnoreCase.

Per forzare la distinzione tra maiuscole e minuscole, posso eseguire (?-i)taylor.

Sembra che le altre opzioni includono:

  • i, caso modalità insensitive
  • s, singola linea
  • m, la modalità multi linea
  • x, modo di spaziatura libera
+0

Come posso aggiungere questo a un espressione come questa: public const string Url = @ "^ (? :(?: https? | ftp): \/\ /) (?: \ S + (? :: \ S *)? @)? (? :(?! (?: 10 | 127): {3}) | ((\ \ d {1,3}?.) ((?: 169 \ .254 192 \ .168?!):?. \ \ d { 1,3}) {2}) (?! 172 \ (?: 1 [6-9] | 2 \ d | 3 [0-1]) (:.?. \ \ d {1,3}) { 2})? (: [1-9] \ d?| 1 \ d \ d | 2 [01] \ d | 22 [0-3]) (:?.? \ (?: 1 \ d {1,2} | 2 [0-4] \ d | 25 [ 0-5])) {2} (: \ (:?.?? [1-9] \ d | 1 \ d \ d | 2 [0-4] \ d | 25 [0-4])) | (: (: [az \ u00a1- \ uffff0-9] - *) * [az \ u00a1- \ uffff0-9] +??) (:?.? \ (: [az \ u00a1- \ uffff0-9] ?.?.?? - *) * [az \ u00a1- \ uffff0-9] +) * (: \ (: [az \ u00a1- \ uffff] {2,})) \) (:: \ d { 2,5}) (: [/ #?] \ S *) $ ";??? – Yovav

24

Come dice spoon16, è (?i). MSDN ha una lista di regular expression options che include un esempio di utilizzo corrispondenza case-insensitive solo parte della partner:

string pattern = @"\b(?i:t)he\w*\b"; 

Qui la "t" viene abbinata case-sensitive, ma il resto è case-sensitive . Se non si specifica una sottoespressione, l'opzione viene impostata per il resto del gruppo che lo contiene.

Così, per il tuo esempio, si potrebbe avere:

string pattern = @"My name is (?i:taylor)."; 

Ciò corrisponde "Il mio nome è Taylor", ma non "MY NAME IS taylor".

47

Come già notato, (?i) è l'equivalente in linea di RegexOptions.IgnoreCase.

Proprio A proposito, ci sono alcuni trucchi che si possono fare con esso:

Regex: 
    a(?i)bc 
Matches: 
    a  # match the character 'a' 
    (?i) # enable case insensitive matching 
    b  # match the character 'b' or 'B' 
    c  # match the character 'c' or 'C' 

Regex: 
    a(?i)b(?-i)c 
Matches: 
    a  # match the character 'a' 
    (?i)  # enable case insensitive matching 
    b  # match the character 'b' or 'B' 
    (?-i) # disable case insensitive matching 
    c  # match the character 'c' 

Regex:  
    a(?i:b)c 
Matches: 
    a  # match the character 'a' 
    (?i: # start non-capture group 1 and enable case insensitive matching 
     b  # match the character 'b' or 'B' 
    )  # end non-capture group 1 
    c  # match the character 'c' 

E si può anche combinare le bandiere in questo modo: a(?mi-s)bc significato:

a   # match the character 'a' 
(?mi-s) # enable multi-line option, case insensitive matching and disable dot-all option 
b   # match the character 'b' or 'B' 
c   # match the character 'c' or 'C' 
Problemi correlati