根据格式验证字符串

Die*_*ego 9 c# string

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

XXXX-XX-XXX-XXXX-XXXXXXXXXX-X

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

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

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

das*_*ght 14

这个regexp应该可以解决问题.它使用负向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
Run Code Online (Sandbox Code Playgroud)

这是一个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-"));
Run Code Online (Sandbox Code Playgroud)


Ode*_*ded 7

使用正则表达式.

^\d[-0-9]+\d$
Run Code Online (Sandbox Code Playgroud)

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

分解:

^    - 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
Run Code Online (Sandbox Code Playgroud)

您可以更改+to *以使2位数字符串有效,并添加一个替换以使1位数字符串有效:

^(\d|\d[-0-9]*\d)$
Run Code Online (Sandbox Code Playgroud)

注:在.NET中,\d将匹配任何 Unicode的数字(所以,例如,阿拉伯数字将匹配) -如果你不希望出现这种情况,则更换\d[0-9]在每一个地方.


Mas*_*uso 5

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

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

^ ---->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. 
Run Code Online (Sandbox Code Playgroud)

更多信息在

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