正则表达式匹配严格的n字符包括新行字符

时间:2017-02-17 13:01:36

标签: regex

我想从文件中逐个验证我的输入记录。我的文件可以包含10,000到20,000条记录

记录只能有(大写aplphabets, - ,点,空格和数字)。记录以新行字符结束。它可能是其中之一(\ n或\ r \ n)

我希望正则表达式匹配只有五个以上参数的记录,包括两种类型的新字符(\ n或\ r \ n)。如果记录包含我提到的其他字符,则不应该匹配。

我试过这个正则表达式。

[A-Z\d\- ]{120}\s+$

让我们举一个10个字符的例子。

1) Input
AAAA12.0 A\nor\r\n

Regex should match for given input(1) because of exact ten characters plus new line character(one is possible at a time)

2)Input
AA-A13.0 AAA\nor\r\n

Regex should match for given input(2) because number of characters are more than 10

但这个正则表达式有时会失败。关于这个正则表达式的任何建议都要改进并使其对我的五个要求更严格吗?

1 个答案:

答案 0 :(得分:0)

这个表达式:

^[-.A-Z \d]*\r?$

如果整行仅由连字符,点,大写字母A-Z,空格和数字组成,则匹配,并以\n或可选\r\n结尾。

相关问题