根据格式验证字符串

时间:2012-05-17 13:05:11

标签: c# string

我的字符串必须采用以下格式:

XXXX-XX-XXX-XXXX-XXXXXXXXXX-X

其中X是整数。整数的数量无关紧要。我只需要确保字符串:

  • 以整数开头和结尾
  • 仅包含用短划线分隔的整数

最简单的验证方法是什么?

4 个答案:

答案 0 :(得分:14)

这个正则表达式应该可以解决问题。它使用负lookbehind来避免连续匹配多个破折号。

^\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

这是一个C#代码,说明了它的用法(也在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-"));

答案 1 :(得分:7)

使用正则表达式。

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

这假设字符串长度至少为三个字符。

故障:

^    - 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

您可以将+更改为*以使2位数字符串有效,并添加更改以使1位数字符串有效:

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

注意:在.NET中,\d将匹配任何 Unicode数字(例如,阿拉伯数字将匹配) - 如果您不想这样,请替换{{1每个地方都有\d

答案 2 :(得分:4)

你可以编写一个能够解决问题的正则表达式。

您可以使用该正则表达式来验证字符串

^ ---->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. 

的更多信息

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

答案 3 :(得分:1)

正则表达式可能是有用的方法:

http://www.regular-expressions.info/creditcard.html