Please help me correct this regular expression in C# to match/validate only when the following is true:
请帮助我更正c#中的这个正则表达式,只有当以下内容为真时才匹配/验证:
- Always starts with
da/
- 总是从da /开始
- At least one single character after
da/
- da/之后至少有一个字符
- Only non capitals are allowed, range from a-z (both included)
- 只允许非大写字母,范围从a-z(包括)
- digits 0-9 allowed
- 数字0 - 9允许
- dashes are allowed (-)
- 允许破折号(-)
This is what I have, but it's not working:
这就是我所拥有的,但它不起作用:
/^da/+[a-z0-9+-]+$/
Example of accepted string that will validate the regular expression:
用于验证正则表达式的可接受字符串示例:
da/this-will-validate-correct-1
2 个解决方案
#1
2
Your regex allows 1 or more /
after da
and the +
inside the character class allowed +
symbols.
您的正则表达式允许一个或多个/在da和+内的字符类允许+符号。
Judging by the requirements, you just need
根据需求判断,您只需要
^da/[a-z0-9-]+$
See the regex demo
看到regex演示
The +
after the character class [a-z0-9+-]
requires at least 1 character after da/
.
字符类[a-z0-9+-]后面的+在da/之后至少需要一个字符。
Regex.IsMatch("da/this-will-validate-correct-1", @"^da/[a-z0-9-]+$")
See the C# demo
查看演示c#
Pattern explanation:
模式说明:
-
^
- start of string - ^ -字符串的开始
-
da/
- a literal string of charactersda/
- da/ -一个字串字符da/。
-
[a-z0-9-]+
- 1 or more characters froma-z
and0-9
ranges or a-
- [a-z0-9-]+ - 1或以上a-z和0-9的字符或a-
-
$
- end of string. - $ -字符串的末端。
#2
0
you can try this ^da/[a-z0-9\-]+$
你可以试试这个^ da / a-z0-9 \[-]+ $
#1
2
Your regex allows 1 or more /
after da
and the +
inside the character class allowed +
symbols.
您的正则表达式允许一个或多个/在da和+内的字符类允许+符号。
Judging by the requirements, you just need
根据需求判断,您只需要
^da/[a-z0-9-]+$
See the regex demo
看到regex演示
The +
after the character class [a-z0-9+-]
requires at least 1 character after da/
.
字符类[a-z0-9+-]后面的+在da/之后至少需要一个字符。
Regex.IsMatch("da/this-will-validate-correct-1", @"^da/[a-z0-9-]+$")
See the C# demo
查看演示c#
Pattern explanation:
模式说明:
-
^
- start of string - ^ -字符串的开始
-
da/
- a literal string of charactersda/
- da/ -一个字串字符da/。
-
[a-z0-9-]+
- 1 or more characters froma-z
and0-9
ranges or a-
- [a-z0-9-]+ - 1或以上a-z和0-9的字符或a-
-
$
- end of string. - $ -字符串的末端。
#2
0
you can try this ^da/[a-z0-9\-]+$
你可以试试这个^ da / a-z0-9 \[-]+ $