2012-05-17 14 views
8

Ho una stringa che deve essere il seguente formato:stringa validate basato su un formato

XXXX-XX-XXX-XXXX-XXXXXXXXXX-X

dove X è un numero intero. Il numero di numeri interi non ha importanza. Ho solo bisogno di fare in modo che la stringa:

  • inizia e finisce con un intero
  • contiene solo numeri interi separati da trattini

quale sarebbe il modo più semplice per verificare che?

+4

Le espressioni regolari faranno il lavoro. – ken2k

risposta

13

Questa espressione regolare dovrebbe fare il trucco. Utilizza un valore negativo lookbehind per evitare la corrispondenza di più trattini di fila.

^\d(\d|(?<!-)-)*\d$|^\d$ 

^  ^ ^^
|  |  | -- is a single digit, or 
|  |  ------- ends with a digit 
|  ----------------consists on digits or dashes not preceded by dashes 
---------------------starts with a digit 

Qui è un codice C# che illustra il suo utilizzo (anche su ideone):

var r = new Regex("^\\d(\\d|(?<!-)-)*\\d$|^\\d$"); 
Console.WriteLine(r.IsMatch("1-2-3")); 
Console.WriteLine(r.IsMatch("1-222-3333")); 
Console.WriteLine(r.IsMatch("123")); 
Console.WriteLine(r.IsMatch("1-2-3-")); 
Console.WriteLine(r.IsMatch("1")); 
Console.WriteLine(r.IsMatch("-11-2-3-")); 
+0

hai un extra) sulla tua risposta. La corretta sarebbe^\ d (\ d | (? Diego

+0

@Diego Hai ragione, l'esempio che ho collegato a ideone non ha questa parentesi in più. Ho modificato la risposta, grazie! – dasblinkenlight

+0

Ho accettato la tua risposta perché era quella che si occupa di daseh di fila. grazie mille – Diego

4

puoi scrivere un'espressione regolare che fa il trucco.

di quanto si possa utilizzare tale espressione regolare per convalidare la stringa

^ ---->Start of a string. 
$ ---->End of a string. 
. ----> Any character (except \n newline) 
{...}----> Explicit quantifier notation. 
[...] ---->Explicit set of characters to match. 
(...) ---->Logical grouping of part of an expression. 
* ---->0 or more of previous expression. 
+ ---->1 or more of previous expression. 
? ---->0 or 1 of previous expression; also forces minimal matching when an expression might match several strings within a search string. 
\ ---->Preceding one of the above, it makes it a literal instead of a special character. Preceding a special matching character, see below. 
\w ----> matches any word character, equivalent to [a-zA-Z0-9] 
\W ----> matches any non word character, equivalent to [^a-zA-Z0-9]. 
\s ----> matches any white space character, equivalent to [\f\n\r\v] 
\S----> matches any non-white space characters, equivalent to [^\f\n\r\v] 
\d ----> matches any decimal digits, equivalent to [0-9] 
\D----> matches any non-digit characters, equivalent to [^0-9] 

\a ----> Matches a bell (alarm) \u0007. 
\b ----> Matches a backspace \u0008 if in a [] character class; otherwise, see the note following this table. 
\t ---->Matches a tab \u0009. 
\r ---->Matches a carriage return \u000D. 
\v ---->Matches a vertical tab \u000B. 
\f ---->Matches a form feed \u000C. 
\n ---->Matches a new line \u000A. 
\e ---->Matches an escape \u001B 

$number ----> Substitutes the last substring matched by group number number (decimal). 
${name} ----> Substitutes the last substring matched by a (?) group. 
$$ ----> Substitutes a single "$" literal. 
$& ----> Substitutes a copy of the entire match itself. 
$` ----> Substitutes all the text of the input string before the match. 
$' ----> Substitutes all the text of the input string after the match. 
$+ ----> Substitutes the last group captured. 
$_ ----> Substitutes the entire input string. 

(?(expression)yes|no) ----> Matches yes part if expression matches and no part will be ommited. 

più Info a

http://geekswithblogs.net/brcraju/archive/2003/10/23/235.aspx

7

usare un'espressione regolare.

^\d[-0-9]+\d$ 

Questo presuppone che la stringa sia lunga almeno tre caratteri.

Ripartizione:

^ - match start of string 
\d - match a digit 
[ - start of character class containing: 
-  - a dash 
0-9 - 0 to 9 
] - end of character class 
+ - match one or more of the previous 
\d - match a digit 
$ - match end of string 

È possibile modificare il +-* fare 2 stringhe di cifre valide, e aggiungere un alternarsi di fare 1 stringhe di cifre valide così:

^(\d|\d[-0-9]*\d)$ 

Nota: .NET, \d corrisponderà a qualsiasi cifra Unicode (quindi, ad esempio, le cifre arabe corrisponderanno) - se non lo si desidera, sostituire \d con [0-9] in ogni luogo.

+0

+1 molto bene, grazie. Unico problema è che non convalida 1 o 12 – Diego

+0

@Diego - No, ho postato il presupposto che ciò presuppone 3 caratteri. È possibile utilizzare l'alternanza per la corrispondenza per 1 e 2 cifre. Risposta aggiornata – Oded

+0

grazie Oded. Solo per le tue informazioni, convalida ancora 1--1 (trattini di fila). Ma va bene, dasblinkenlight ha già pubblicato una soluzione funzionante. Grazie ancora per il tuo tempo – Diego

Problemi correlati