子字符串上的字母数字正则表达式

发布于 2025-01-01 21:50:33 字数 353 浏览 2 评论 0原文

我知道正则表达式和子字符串是这里的一个常见问题,但我似乎无法将我正在阅读的内容与实际应用程序关联起来。

我想要做什么:

获取一个字符串,查看该字符串的最后 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

将军与妓 2025-01-08 21:50:33

您需要确保最后 16 个字符是字母数字吗?只需使用这个正则表达式:

[a-zA-Z0-9]{16}$

您现在遇到的问题是 .Substring(16) 将返回字符串中位于位置 16 之后(包括位置 16)的所有字符 - 而不是最后 16 个字符人物。此外,您已经不区分大小写,因此:

if(Regex.IsMatch(STRINGTOCHECK, @"[a-zA-Z0-9]{16}$"))

最后的 $ 锚点确保最后 16 个字符匹配。

You need to make sure the last 16 characters are alphanumeric? Just use this regular expression:

[a-zA-Z0-9]{16}$

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:

if(Regex.IsMatch(STRINGTOCHECK, @"[a-zA-Z0-9]{16}$"))

The final $ anchor makes sure the last 16 characters are being matched.

無處可尋 2025-01-08 21:50:33

Substring 的参数是开始的索引。

因此,要获取最后 16 个字符的子字符串,需要从字符串长度中减去 16。

string last16 = STRINGTOCHECK.Substring(STRINGTOCHECK.Length - 16);
if(Regex.IsMatch(last16, @"^[a-zA-Z0-9]*$")
{

}

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.

string last16 = STRINGTOCHECK.Substring(STRINGTOCHECK.Length - 16);
if(Regex.IsMatch(last16, @"^[a-zA-Z0-9]*$")
{

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文