This question already has answers here:
Regular expression to enforce complex passwords, matching 3 out of 4 rules
(4 answers)
Closed 8 years ago.
I would like to use a regular expression in the ASP.NET membership. What is a regular express for the below?
at least 8 characters long
include at least one upper case letter
one lower case letter
one number
try this..
^((?=.*\d)(?=.*[A-Z])(?=.*[a-z]).{8,})
You could use something like that:
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z\d=:;<>,~!##\\$%/^&)(\[\]+-]{8,}$
Test it here.
You may also want to learn about the "?=" thing, which is called "positive lookahead" here.
In short, when all three lookaheads (.*\d and .*[a-z] and .*[A-Z]) are matched (and are discarded), the main regex [a-zA-Z\d=:;<>,~!##\\$%/^&)(\[\]+-]{8,} can be matched too.
Do you have to do this in one regex? I would make each of those rules one regex, and test for them individually. I suspect you code will end up being simpler, and you'll save yourself and whoever has to maintain your application several headaches.
Related
This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 5 years ago.
I want to validate these formats in one regular expression in asp.net:
XX-XXXXXXX or XXX-XX-XXXX
These have to be numeric only no characters except the "-".
Is this possible? I've been trying without any success so I want to ask the experts.
Thanks,
Pune
The following should work given your requirements.
"(^\d{2}-\d{7}$)|(^\d{3}-\d{2}-\d{4}$)"
Try something like this:
/^([0-9]{2}-[0-9]{7}|[0-9]{2}-[0-9]{2}-[0-9]{4})$/
[0-9] means any character from 0 to 9.
{X} means X times
| means "or"
- means "-"
and ( and ) delimits a group for replacing
^ and $ delimit the beginning and the ending of the match.
This question already has answers here:
Assign multiple new variables on LHS in a single line
(15 answers)
Closed 9 years ago.
In python it is possible to decompose a list
x=[1,2,3]
a,b,c=x # a=1 b=2 c=3
is it possible to do something similar in R?
for example something like:
x=matrix(rnorm(100),10,10)
[u d v]=svd(x) # instead of u=svd$u d=svd$d v=svd$v
Not without some hackery and fiddly global assignment, and my personal opinion is that this is not a good thing to do.
Why? Well, if the results from svd (to use your example) are returned together there's a good reason they are returned together - they belong together. Once you break it up you lose that relationship. The only win is having fewer characters to type, and that's not one of the things we optimise with programming - readability should win over that.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the most efficient way to reach a spot on sight on VIM?
With the code like this:
[caret here]create_stage :pay_deposit, due: deposit_due_date, actual: deposit_paid_date, action: 'Deposit', status: status
I want to jump right to/before/after the Nth comma (or at least around) of actual: deposit_paid, [need to be here], action: 'etc'
What is the most efficient way of doing it? (I currently just w-w-w which sucks, also could start counting number of words to use something like 12w but that just distracts too much).
I don't want search since I do want to keep current search and highlighting.
You can combine the f motion with a [count], so for your example, 3f, would be the shortest way. (And if you have miscounted, you can correct with ; and ,.)
f,;;; or use easymotion and probable dupe of What is the most efficient way to reach a spot on sight on VIM?
What about searching the comma character?
/, and then input a number, say 3 and then type n or N to locate all commas in the doc?
Should be validating 1-280 input characters, but it hangs when more than 280 characters are input.
Clarification
I am using the above regex to validate the length of input string to be 280 characters maximum.
I am using asp:RegularExpressionValidator to do that.
There's nothing “wrong” with it per se, but it's horrendous because with most RE engines (you don't say which one you're using) when it doesn't match with the first thing it tries because it causes the engine to backtrack and try loads of different possibilities (none of which can ever cause a match). So it's not a hang, but rather just a machine that's trying to execute around 2280 operations to see if there's a match possible. Excuse me if I don't wait around for that!
Of course, it's theoretically possible for the RE compiler to merge the (.|\s) part of the RE into something it doesn't need to backtrack to deal with. Some RE engines do this (typically the more automata-theoretic ones) but many don't (the stack-based ones).
It is trying every possible combination of . and \s for each character trying to find a version of the pattern that matches the string.
. already matches any character, so (.|\s) is redundant. Further, if you just want to check what the length of the string is, then just do that - why are you pulling out regexes?
If you really want to use a regular expression, you could use .{1, 280}$ combined with the SingleLine option, so that the . metacharacter will match everything, including new lines (see here, Regular Expression API section).
I want to use an ASP.NET RegularExpressionValidator to limit the number of words in a text box. (The RegularExpressionValidator is my favoured solution because it will do both client and server side checks).
So what would be the correct Regex to put in the RegularExpressionValidator that will count the words and enforce a word-limit? For, lets say, 150 words.
(NB: I see that this question is similar, but the answers given seem to also rely on code such as Split() so I don't think any of them could plug into a RegularExpressionValidator which is why I'm asking again)
Since ^ and $ is implicitly set with RegularExpressionValidators, use the following:
(\S*\s*){0,10}
The 0 here allows empty strings (more specifically 0 words) and 150 is the max number of words to accept. Adjust these as necessary to increase/decrease the number of words accepted.
The above regex is non-greedy, so you'll get a quicker match verses the one given in the question you reference. (\b.*\b){0,10} is greedy, so as you increased the number of words you'll see a decrease in performance.
Here is a quick reference for regular expressions:
http://msdn.microsoft.com/en-us/library/az24scfc.aspx
You can use this site to test the expressions:
http://regexpal.com/
Here is my regex example that works with both minimum and maximum word count (and fixes bug with leading spacing):
^\s*(\S+\s+|\S+$){10,150}$
Check this site:
http://lawrence.ecorp.net/inet/samples/regexp-validate.php#count
It's JavaScript RegEx, but it's very similar to asp.net
It's something like this:
(\b[a-z0-9]+\b.*){4,}