9

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

XXXX-XX-XXX-XXXX-XXXXXXXXXX-X

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

  • 以整数开头和结尾
  • 仅包含用破折号分隔的整数

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

4

4 回答 4

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-"));
于 2012-05-17T13:08:54.500 回答
8

使用正则表达式。

^\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 数字(例如,阿拉伯数字将匹配) - 如果您不希望这样,请在每个地方替换\d为。[0-9]

于 2012-05-17T13:09:25.840 回答
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. 

更多信息在

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

于 2012-05-17T13:07:51.363 回答
1

正则表达式可能是可行的方法,这可能会有所帮助:

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

于 2012-05-17T13:08:52.783 回答