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!
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.