字符串必须是字母数字并且包含某个子字符串
String must be alphanumeric and contain a certain substring
我正在努力添加一个正则表达式来确定给定的输入是否有效。输入应为字母数字(也允许使用下划线、破折号、句点),并且介于 1 到 60 个字符之间。它还应该在其中包含某个子字符串(假设为“foo.bar”)。这是我的尝试:
^.[a-zA-Z0-9_.-]{1,60}$
除了子字符串部分,这就是我需要的。我不确定如何添加“字符串必须包含子字符串 foo.bar”要求。 FWIW 我在 Ruby 中这样做,所以我知道这意味着正在使用 PCRE。
例如,这个字符串应该是有效的:
aGreatStringWithfoo.barInIt1111
这不应该
aBadStringWithoutTheSubstringInIt
使用
^(?=.{1,60}$)[a-zA-Z0-9_.-]*foo\.bar[a-zA-Z0-9_.-]*$
参见regex proof。
解释
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.{1,60} any character except \n (between 1 and
60 times (matching the most amount
possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of
the string
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
[a-zA-Z0-9_.-]* any character of: 'a' to 'z', 'A' to 'Z',
'0' to '9', '_', '.', '-' (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
foo 'foo'
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
bar 'bar'
--------------------------------------------------------------------------------
[a-zA-Z0-9_.-]* any character of: 'a' to 'z', 'A' to 'Z',
'0' to '9', '_', '.', '-' (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
我正在努力添加一个正则表达式来确定给定的输入是否有效。输入应为字母数字(也允许使用下划线、破折号、句点),并且介于 1 到 60 个字符之间。它还应该在其中包含某个子字符串(假设为“foo.bar”)。这是我的尝试:
^.[a-zA-Z0-9_.-]{1,60}$
除了子字符串部分,这就是我需要的。我不确定如何添加“字符串必须包含子字符串 foo.bar”要求。 FWIW 我在 Ruby 中这样做,所以我知道这意味着正在使用 PCRE。
例如,这个字符串应该是有效的:
aGreatStringWithfoo.barInIt1111
这不应该
aBadStringWithoutTheSubstringInIt
使用
^(?=.{1,60}$)[a-zA-Z0-9_.-]*foo\.bar[a-zA-Z0-9_.-]*$
参见regex proof。
解释
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.{1,60} any character except \n (between 1 and
60 times (matching the most amount
possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of
the string
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
[a-zA-Z0-9_.-]* any character of: 'a' to 'z', 'A' to 'Z',
'0' to '9', '_', '.', '-' (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
foo 'foo'
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
bar 'bar'
--------------------------------------------------------------------------------
[a-zA-Z0-9_.-]* any character of: 'a' to 'z', 'A' to 'Z',
'0' to '9', '_', '.', '-' (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string