8

I need a regular expression that will match a string that contains:

  • at least one number
  • zero or more letters
  • no other characters such as spaces

The string must also be a minimum of 8 characters and a maximum of 13 characters.

Placement of the numbers and/or letters within the 8-13 character string does not matter. I haven't figured out how to make sure that the string contains a number, but here are some expressions that don't work because they are picking up spaces in the online tool Regexr. Take a look below:

 - ([\w^/s]){8,13}
 - ([a-zA-Z0-9]){8,13}
 - ([a-zA-Z\d]){8,13}

I am specifically looking to exclude spaces and special characters. The linked and related questions all appear to allow for these characters. This is not for validating passwords, it is for detecting case numbers in natural language processing. This is different from "Password REGEX with min 6 chars, at least one letter and one number and may contain special characters" because I am looking for at least one number but zero or more letters. I also do not want to return strings that contain any special characters including spaces.

vikrant rana
  • 4,509
  • 6
  • 32
  • 72
user4735294
  • 85
  • 1
  • 1
  • 5
  • I am specifically looking to exclude spaces and special characters. The linked and related questions all appear to allow for these characters. This is not for validating passwords, it is for detecting case numbers in natural language processing. – user4735294 Jan 28 '16 at 18:21
  • Ten million password validation regex questions answered on SO, yet this one is a _Duplicate_ ? You've got to be kidding.. –  Jan 28 '16 at 18:36
  • 1
    Use this `^(?=.*\d)[a-zA-Z\d]{8,13}$` –  Jan 28 '16 at 18:39
  • thank you so much @sln, that worked!! I don't see any option to upvote your answer. – user4735294 Jan 28 '16 at 19:31
  • @sln: Post your answer. – Wiktor Stribiżew Jan 28 '16 at 20:41
  • Possible duplicate of [Regex for Password Must be contain at least 8 characters, least 1 number and both lower and uppercase letters and special characters](http://stackoverflow.com/questions/19605150/regex-for-password-must-be-contain-at-least-8-characters-least-1-number-and-bot) – miken32 Jan 28 '16 at 21:16
  • @WiktorStribiżew - I posted it. Btw, why did you change your user name? –  Jan 29 '16 at 00:32
  • I have never set it before, thus I changed the one set by SO system. – Wiktor Stribiżew Jan 29 '16 at 06:40

2 Answers2

14

This is a typical password validation with your requirements.
Note that this will also match 8-13 digits as well (but it is requested).

Ten million + 1 (and counting) happy customers ..

^(?=.*\d)[a-zA-Z\d]{8,13}$

Explained

 ^                     # Beginning of string
 (?= .* \d )           # Lookahead for a digit
 [a-zA-Z\d]{8,13}      # Consume 8 to 13 alphanum characters
 $                     # End of string
  • thank you. my first issue was that i was picking up spaces. the answer looks similar to my third attempt, but i see now that i had parentheses around the 'consume alpahnum' part, and that probably caused the problem. – user4735294 Jan 29 '16 at 15:06
  • Given the purpose of matching strings in a document, anchoring with word boundaries (\b or similar, maybe space+punct char classes) might be more useful than carat/dollar. :) – dannysauer Feb 03 '16 at 13:27
  • @dannysauer - The `^$` is a catch-all boundary construct. It says: "Because people don't understand regex enough yet to interpret complicated boundary concepts, the basic start/end anchors are used." –  Feb 04 '16 at 20:55
  • Are you allowed to use a zero-width lookahead assertion and still claim simplicity? ;) – dannysauer Feb 04 '16 at 21:07
  • @dannysauer - Actually, any construct that operates between characters is a complex entity. The phrase `zero-width` is bulls**t since the content of the construct is matched the same as anything else but on a different frame. –  Feb 04 '16 at 21:22
0

I've seen the answer above (by sln) everywhere over the internet, but as far as I can tell, it is NOT ACCURATE.

If your string contains 8 to 13 characters with no numbers this expression will match it, because it uses the * quantifier on the wildcard character . in the positive lookahead.

In order to match at least 1 digit, 1 A-Z and 1 a-z in a password that's at least 8 characters long, you'll need something like this:

(?=.{1,7}\d)(?=.{1,7}[a-z])(?=.{1,7}[A-Z])[a-zA-Z\d]{8,13}

it uses 3 lookaheads:

(?=.{1,7}\d)
(?=.{1,7}[a-z])
(?=.{1,7}[A-Z])

each time, it looks for the target (eg the first digit) but allows 1 to 7 occurances of any character before it. Then it will match 8 to 13 alphanumeric characters.

NOTE to Powershell users: Use a search group to be able to extract a result

$password = [regex]::match($string-to-search,'(?=.{1,7}\d)(?=.{1,7}[a-z])(?=.{1,7}[A-Z])([a-zA-Z\d]{8,13})').Groups[1].Value
Valentino
  • 465
  • 6
  • 17
  • There're no needs for 3 lookaheads. Moreover, your regex matches `%*!:;123abcABC-'"` that is not wanted but doesn't match `123456789` that is wanted. [Demo](https://regex101.com/r/T6nGJK/1). Reread the question. – Toto Sep 04 '19 at 14:29