Highlighted

Regex - string must contain 6-20 characters, with at least 2 digits (don't have to be consecutive) and any number of alphabetic characters

New Here ,
Nov 18, 2015

Copy link to clipboard

Copied

I am trying to write  a regex expression to match a string that is between 6 and 20 characters long, has at least 2 digits that do not have to be consecutive, and any number of alphabetic characters that do not have to be consecutive. All non-case sensitive. I've gotten this far, but it's still not working:

^(?=\D*\d{2,}).{6,20}$

Should match "abc3def1", but it doesn't because the digits aren't consecutive even though there are two of them. This does match: "abc34def1".

Please help!

Views

250

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Regex - string must contain 6-20 characters, with at least 2 digits (don't have to be consecutive) and any number of alphabetic characters

New Here ,
Nov 18, 2015

Copy link to clipboard

Copied

I am trying to write  a regex expression to match a string that is between 6 and 20 characters long, has at least 2 digits that do not have to be consecutive, and any number of alphabetic characters that do not have to be consecutive. All non-case sensitive. I've gotten this far, but it's still not working:

^(?=\D*\d{2,}).{6,20}$

Should match "abc3def1", but it doesn't because the digits aren't consecutive even though there are two of them. This does match: "abc34def1".

Please help!

Views

251

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Nov 18, 2015 0
Advocate ,
Nov 18, 2015

Copy link to clipboard

Copied

Is this a class project?

Are you limited to a single expression? If not, while it can be done, I would prefer to break it up into two expressions to make it easier for others and yourself to support in the future. Test for your 6-20 characters first and then pull out the numbers and check for 2+.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 18, 2015 0
New Here ,
Nov 18, 2015

Copy link to clipboard

Copied

Yes, a class project, so it all has to happen together unfortunately. Thank you for any help!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 18, 2015 0
Advocate ,
Nov 19, 2015

Copy link to clipboard

Copied

Ok. Figure it out. You're learning. Googling for a quick answer or asking someone else to do your work might get you an easy A in the short term, but it'll take you much longer to become a programmer -- assuming that is your goal.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 19, 2015 0