Regular expression to allow only one space between words - asp.net

I've a textbox in an ASP.NET application, for which I need to use a regular expression to validate the user input string. Requirements for regex are -
It should allow only one space between words. That is, total number of spaces between words or characters should only be one.
It should ignore leading and trailing spaces.
Matches:
Test
Test abc
Non Matches:
Test abc def
Test abc --> I wanted to include multiple spaces between the 2 words. However the editor ignores these extra spaces while posting a question.

Assuming there must be either one or two 'words' (i.e. sequences of non-space characters)
"\s*\S+(\s\S+)?\s*"
Change \S to [A-Za-z] if you want to allow only letters.

Pretty straightforward:
/^ *(\w+ ?)+ *$/
Fiddle: http://refiddle.com/gls

Maybe this one will do?
\s*\S+?\s?\S*\s*
Edit: Its a server-encoded regex, meaning that you might need to remove one of those escaping slashes.

How about:
^\s*(\w+\s)*\w+\s*$

Related

Regex in asp.net for allowing only characters with single space?

I want to allow a user to enter only characters and with single space in a textbox. What i have tried is :
^[\w\.:\(\)\[\]{}\-_](?: ?[\w\.:\(\)\[\]{}\-_])*$
it is blocking all but allowing digits also . How to make it correct such that it only allow characters and no digits but a single space between words? Thank you.
I'd use:
^[\p{L}.:()[\]{}_-]+(?: [\p{L}.:()[\]{}_-]+)*$
Where \p{L} stands for any letter.
Edit:
I've changed \pL to \p{L} because it's not supported by .NET.
Thanks to Alan Moore.
Is ([A-Za-z])+( [A-Za-z]+) what you're after? Or do you want the regex to only match once?
Assuming there must be either oneor two 'words' (i.e. sequences of non-space characters)
"\s*[A-Za-z] +(\s[A-Za-z] +)?\s*"

Regular Expression for username and password?

I am trying to use a regular expression for name field in the asp.net application.
Conditions:name should be minimum 6 characters ?
I tried the following
"^(?=.*\d).{6}$"
I m completely new to the regex.Can any one suggest me what must be the regex for such condition ?
You could use this to match any alphanumeric character in length of 6 or more: ^[a-zA-Z0-9]{6,}$. You can tweak it to allow other characters or go the other route and just put in exclusions. The Regex Coach is a great environment for testing/playing with regular expressions (I wrote a blog post with some links to other tools too).
Look at Expression library and choose user name and/or password regex for you. You can also test your regex in online regex testers like RegexPlanet.
My regex suggestions are:
^[a-zA-Z][a-zA-Z0-9._\-]{5,}$
This regex accepts user names with minimum 6 characters, starting with a letter and containing only letters, numbers and ".","-","_" characters.
Next one:
^[a-zA-Z0-9._\\-]{6,}$
Similar to above, but accepts ".", "-", "_" and 0-9 to be first characters too.
If you want to validate only string length (minimum 6 characters), this simple regex below will be enough:
^.{6,}$
What about
^.{6,}$
What's all the stuff at the start of yours, and did you want to limit yourself to digits?
NRegex is a nice site for testing out regexes.
To just match 6 characters, ".{6}" is enough
In its simplest form, you can use the following:
.{6,}
This will match on 6 or more characters and fail on anything less. This will accept ANY character - unicode, ascii, whatever you are running through. If you have more requirements (i.e. only the latin alphabet, must contain a number, etc), the regex would obviously have to change.

Problem with an asp net password regular expression?

I'm trying to validate the following in a password field:
-at least 1 alpha
-at least 1 numeric
-at least 1 special (non alphanumeric)
My reg exp is this:
Regex.IsMatch("jpere33z#1?hs", #"^\w*(?=\w*\d)(?=\w*[a-z])(?=\W*)\w*$")
and it says it is not valid. The \W part is what is not working.
Could you please tell me why?
\w*$ will only match letters, numbers, and underscore. This is what you want:
Regex.IsMatch("#1?hsjpere33z", #"^(?=.*?\d)(?=.*?[a-z])(?=.*?\W).*$", RegexOptions.IgnoreCase)
I moved the validation to the left, and added \w* right before the \W.
Edit: Also used .* instead of \w for test lookaheads.
Your regex doesn't allow more then 1 digit.
Your easiest route would probably be to have 3 regex checks, one for the existance of each character type.
This is difficult to do with regex (at least only one). In the regex you are giving the fields an order, so the parser expects them in that order.
One alternative would be to use a choice, but that would make difficult to check that you have one of each of the terms:
[\w|\d|\W]{4,}
If you want to use regex, check three of them:
1) Is there a digit?
2) Is there a character?
3) Is there a special?
If all of them are true.... bingo!

Difference between (.|[\r\n]){1,1500} and ^.{1,1500}$

What is the difference between below two regular expressions
(.|[\r\n]){1,1500}
^.{1,1500}$
The first matches up-to-1500 chars, and the second (assuming you haven't set certain regex options) matches a first single line of up-to-1500 chars, with no newlines.
. does not match new lines.
The second one matches the first 1500 characteres of a line IF the line contains 1500 characters or less
First expression matches some <= 1500 characters of the file(or other source).
Second expression matches a entire line with charsNumber <= 1500.
. matches any character except \n newline.
If it's for use in a RegularExpressionValidator, you probably want to use this regex:
^[\s\S]{1,1500}$
This is because the regex may be run on either the server (.NET) or the client (JavaScript). In .NET regexes you can use the RegexOptions.Singleline flag (or its inline equivalent, (?s)) to make the dot match newlines, but JavaScript has no such mechanism.
[\s\S] matches any whitespace character or anything that's not a whitespace character--in other words, anything. It's the most popular idiom for matching anything including a newline in JavaScript; it's much, much more efficient than alternation-based approaches like (.|\n).
Note that you'll still need to use a RequiredFieldValidator if you don't want the user to leave the textbox empty.

Regex: Match opening/closing chars with spaces

I'm trying to complete a regular expression that will pull out matches based on their opening and closing characters, the closest I've gotten is
^(\[\[)[a-zA-Z.-_]+(\]\])
Which will match a string such as "[[word1]]" and bring me back all the matches if there is more than one, The problem is I want it to pick up matchs where there may be a space in so for example "[[word1 word2]]", now this will work if I add a space into my pattern above however this pops up a problem that it will only get one match for my entire string so for example if I have a string
"Hi [[Title]] [[Name]] [[surname]], How are you"
then the match will be [[Title]] [[Name]] [[surname]] rather than 3 matches [[Title]], [[Name]], [[surname]]. I'm sure I'm just a char or two away in the Regex but I'm stuck, How can I make it return the 3 matches.
Thanks
You just need to make you regex non-greedy by using a ? like:
^(\[\[)[a-zA-Z.-_ ]+?(\]\])
Also there is a bug in your regex. You've included - in the char class thinking of it as a literal hyphen. But - in a char class is a meta char. So it effectively will match all char between . (period) and _ (underscore). So you need to escape it as:
^(\[\[)[a-zA-Z.\-_ ]+?(\]\])
or you can put is in some other place in the regex so that it will not have things on both sides of it as:
^(\[\[)[a-zA-Z._ -]+?(\]\])
or
^(\[\[)[-a-zA-Z._ ]+?(\]\])
You need to turn off greedy matching. See these examples for different languages:
asp.net
java
javascript
You should use +? instead of +.
The one without the question mark will try to match as much as possible, while the one with the question mark as little as possible.
Another approach would be to use [^\]] as your characters instead of [a-zA-Z.-_]. That way, a match will never extend over your closing brackets.

Resources