2015-02-23 75 views
1

我不得不写一个正则表达式,该表达式将输入字符串限制为最大长度为250个字符,最大长度为7行。这些都需要在一个正则表达式中。REGEX:出现的最大长度和数量

Seperately我会写:

^.{0,250}$ // max length 
^([^\r\n]*[\r\n][^\r\n]*){0,6}$ //maximum seven lines 

使用结合他们 (= ..?)(= ..?)似乎并不在https://www.debuggex.com/

工作有什么办法可以这样在一个正则表达式中完成?

编辑:这是.NET

+1

正则表达式语言大相径庭。请指定您正在使用哪种正则表达式(即,哪种编程语言)的方言。 – 2015-02-23 09:54:44

回答

1

您可以使用此一negative lookahead assertion

(?s)^(?!(?:[^\r\n]*\r?\n){7}).{0,250}$ 

说明:

(?s)  # Mode modifier: Dot matches newlines 
^   # Match start of string 
(?!  # Assert that it's impossible to match... 
(?:  # (Start of group): 
    [^\r\n]* # Any number of characters except newlines 
    \r?\n # followed by one Windows or Mac/Unix newline 
){7}  # repeated seven times 
)   # End of lookahead assertion 
.{0,250} # Match up to 250 characters of any kind 
$   # Match end of string