Need help modifying a regular expression

I have  a regular expression that I need to modify so that it limits character input to 50 characters, and so that it will not allow the user to enter an empty space. Can someone help me modify the regular expression below? I am using the .Net platform with C#. So basically it needs to run on .NET

var r = new Regex(@"((?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[\d]))");
LVL 2
brgdotnetcontractorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
(?=^\S{8,50}$)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dan CraciunIT ConsultantCommented:
var r = new Regex(@"((?=.{8,50})(?=.*[a-z])(?=.*[A-Z])(?=.*[\d])(?!.*\s).+)");

string resultString = null;
try {
	resultString = Regex.Match(subjectString, @"((?=.{8,50})(?=.*[a-z])(?=.*[A-Z])(?=.*[\d])(?!.*\s).+)").Groups[1].Value;
} catch (ArgumentException ex) {
	// Syntax error in the regular expression
}
if (resultString.length > 7) { 
       // you found a match, do something with it
} else {
      // no match, ask the user to input again
}

Open in new window


HTH,
Dan
käµfm³d 👽Commented:
Personally, I'd put the length check on the matched input itself (i.e. not within a lookahead). You can leave the lookaheads for the specific character checks:

^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?\d)[a-zA-Z0-9]{8,50}$

Open in new window


You can adjust the last range to accommodate whatever characters you wish to accept as input. I also adjusted the lookaheads to be non-greedy, since there really is no benefit to having greediness there.
käµfm³d 👽Commented:
Or not. I would point out, however, that the 2nd solution you selected above does not, in fact, limit the input to 50 characters.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.