-6

I was wondering if it is possible to use a regex to check the first n characters of a string and determine if any of them contain a digit?

I realize this can be done by first splitting the string and then using a \d on the substring, but I would like to see a solution where it could be done in one shot

ekjcfn3902039
  • 1,673
  • 3
  • 29
  • 54
  • Yes it is possible to do it with regex and also, without regex. Refer to [link](https://stackoverflow.com/q/19859282/4410922). But SO is not here to provide you solutions – yash Dec 04 '17 at 18:39

1 Answers1

3

This will do it in 1 shot, but not the way you'd expect:

r'\D{n}'

That is, it matches string of length n that does not contain any digits; thus, if the match fails, there must be a digit in the first n characters.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101