子字符串上的字母数字正则表达式
我知道正则表达式和子字符串是这里的一个常见问题,但我似乎无法将我正在阅读的内容与实际应用程序关联起来。
我想要做什么:
获取一个字符串,查看该字符串的最后 16 个字符,并确保它是字母数字。以下是我的想法。
if (Regex.IsMatch(STRINGTOCHECK.ToLower().Substring(16), @"^[a-zA-Z0-9]*$"))
请求
一些代码,向我展示完成此操作的正确方法
或指出我的代码错误的地方
等。
感谢所有帮助!
I know regex and substrings is a common question on here but i can not seem to correlate what I am reading to actual application.
What i want to do:
take a string, look at the last 16 characters of the string, and make sure its alphanumeric. Below is what i have come up with.
if (Regex.IsMatch(STRINGTOCHECK.ToLower().Substring(16), @"^[a-zA-Z0-9]*$"))
request
some code showing me the right way to accomplish this
or pointing out where my code is wrong
etc.
All help is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要确保最后 16 个字符是字母数字吗?只需使用这个正则表达式:
您现在遇到的问题是
.Substring(16)
将返回字符串中位于位置 16 之后(包括位置 16)的所有字符 - 而不是最后 16 个字符人物。此外,您已经不区分大小写,因此:最后的
$
锚点确保最后 16 个字符匹配。You need to make sure the last 16 characters are alphanumeric? Just use this regular expression:
The problem you have right now is that
.Substring(16)
will return all characters in the string after and including position 16 - not the last 16 characters. What's more, you're already case-insensitive, so:The final
$
anchor makes sure the last 16 characters are being matched.Substring
的参数是开始的索引。因此,要获取最后 16 个字符的子字符串,需要从字符串长度中减去 16。
The parameter to
Substring
is the index from which to start.So, to get a substring of the last 16 characters, you need to subtract 16 from the length of the string.