此验证适用于允许使用字母数字字符,空格和短划线,但我无法将最大长度设置为23。
正则表达式: 的(^ \ W + \ S *( - )(\ S * \ W + \ S *)(\ W +)$){0,23}
我需要通过的案件:
我需要失败的案例:
答案 0 :(得分:4)
单独检查长度可能更方便,但您可以使用前瞻来确认整个表达式介于0到23个字符之间。
(?=^.{0,23}$)(^\w+\s*(-?)(\s*\w+\s*)(\w+)$)
答案 1 :(得分:2)
只需使用前瞻来断言最大长度:
(?=^.{1,23}$)^\w+\s*(-?)(\s*\w+\s*)(\w+)$
或者负面的前瞻也是如此:
(?!^.{24,})^\w+\s*(-?)(\s*\w+\s*)(\w+)$
lookaheads现代正则表达式
支持可变宽度most答案 2 :(得分:0)
正则表达式不能像你想要的那样匹配总长度。
使用
procedure TForm2.TMSFMXWebBrowser1BeforeNavigate(Sender: TObject;
var Params: TTMSFMXCustomWebBrowserBeforeNavigateParams);
begin
// Get your result from Params.URL and cancel via Params.Cancel := True;
end;
并在比赛结束后手动检查组#1的长度。
答案 3 :(得分:0)
^(?!(^-|-$|.{24,})).*
Winston1-Salem6 - PASS Winston-Salem - PASS Winston Salem - PASS 1-two3 - PASS word2 with space - PASS -Newberty-Los- - FAIL 12345678901234567890123444 - FAIL
演示 https://regex101.com/r/eM3qR9/2
正则表达式解释:
^(?!(^-|-$|.{24,})).*
Assert position at the beginning of the string «^»
Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!(^-|-$|.{24,}))»
Match the regex below and capture its match into backreference number 1 «(^-|-$|.{24,})»
Match this alternative «^-»
Assert position at the beginning of the string «^»
Match the character “-” literally «-»
Or match this alternative «-$»
Match the character “-” literally «-»
Assert position at the end of the string, or before the line break at the end of the string, if any «$»
Or match this alternative «.{24,}»
Match any single character that is NOT a line break character «.{23,}»
Between 24 and unlimited times, as many times as possible, giving back as needed (greedy) «{24,}»
Match any single character that is NOT a line break character «.*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»