I trying to create custom email regexp
[first]#[second].[third]
Allowed signs are numbers, letters and some special characters (I have done it)
One #
First+second to be from 1 to 20 characters long.
Email can't start or end with dot. Two or more dots next to each other are also not
allowed.
Third string to be 2 or 3 characters long.
This is what I have so far:
^(?!\.)(?=[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+#[a-zA-Z0-9]).{2,21}(?:\.[a-zA-Z0-9].{2,3})$
Unfortunately it doesn't work as I expected. Thanks for any tips.
For instance I can add multiple dots next to each other:
test...s#fm.com
Never ever write a regex to check for e-mails. You'll never make it right. As an example you say:
Allowed signs are numbers, letters and some special characters (I have done it)
which is terribly wrong, because you're then not including unicode characters like å or ţ which are valid. I'm also pretty sure you don't know that # is a valid value within the first part of an e-mail.
Third string to be 2 or 3 characters long.
and what about matching the following tlds: .info, .ninja, .website or .space?
So please, don't.
for reference:
https://davidcel.is/posts/stop-validating-email-addresses-with-regex/
http://stackoverflow.com/questions/23471591/regex-for-iban-allowing-for-white-spaces-and-checking-for-exact-length/23471740#23471740
http://thedailywtf.com/articles/how-to-validate-a-url
Following regex might help you.
^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,3}))$
Please read answer posted by #zmo too.
Related
I am a newbie to regex and would like to create a regular expression to check usernames. These are the conditions:
username must have between 4 and 20 characters
username must not contain anything but letters a-z, digits 0-9 and special characters -._
the special characters -._ must not be used successively in order to avoid confusion
the username must not contain whitespaces
Examples
any.user.13 => valid
any..user13 => invalid (two dots successively)
anyuser => valid
any => invalid (too short)
anyuserthathasasupersuperlonglongname => invalid (too many characters)
any username => invalid because of the whitespace
I've tried to create my own regex and only got to the point where I specify the allowed characters:
[a-z0-9.-_]{4,20}
Unfortunately, it still matches a string if there's a whitespace in between and it's possible to have two special chars .-_ successively:
If anybody would be able to provide me with help on this issue, I would be extremely grateful. Please keep in mind that I'm a newbie on regex and still learning it. Therefore, an explanation of your regex would be great.
Thanks in advance :)
Sometimes writing a regular expression can be almost as challenging as finding a user name. But here you were quite close to make it work. I can point out three reasons why your attempt fails.
First of all, we need to match all of the input string, not just a part of it, because we don't want to ignore things like white spaces and other characters that appear in the input. For that, one will typically use the anchors ^ (match start) and $ (match end) respectively.
Another point is that we need to prevent two special characters to appear next to each other. This is best done with a negative lookahead.
Finally, I can see that the tool you are using to test your regex is adding the flags gmi, which is not what we want. Particularly, the i flag says that the regex should be case insensitive, so it should match capital letters like small ones. Remove that flag.
The final regex looks like this:
/^([a-z0-9]|[-._](?![-._])){4,20}$/
There is nothing really cryptic here, except maybe for the group [-._](?![-._]) which means any of -._ not followed by any of -._.
I am trying to handle Arabic strings.
I want to handle multiple spaces between two strings (i.e. first name, last name).
But the RegEx that I am using is valid only for 1 spacing between the first name and last name.
RegEx used:
/^[\u0600-\u06FF]+([ ][\u0600-\u06FF]+)?$/
Please suggest.
As suggested by Simone Chelo, you need to add "+" to the regex. It means "one or more".
You also don't need to wrap the space with brackets.
This should work for you:
/^[\u0600-\u06FF]+( +[\u0600-\u06FF]+)?$/
If you want any kind of white space, you can use \s instead of [ ]
/^[\u0600-\u06FF]+(\s+[\u0600-\u06FF]+)?$/
Here is a great resource for regex.
I'm trying to create a regular expression in javascript for a UK bank sort code so that the user can input 6 digits, or 6 digits with a hyphen between pairs. For example "123456" or "12-34-56". Also not all of the digits can be 0.
So far I've got /(?!0{2}(-?0{2}){2})(\d{2}(-\d{2}){2})|(\d{6})/ and this jsFiddle to test.
This is my first regular expression so I'm not sure I'm doing it right. The test for 6 0-digits should fail and I thought the -? optional hyphen in the lookahead would cause it to treat it the same as 6 0-digits with hyphens, but it isn't.
I'd appreciate some help and any criticism if I'm doing it completely incorrectly!
Just to answer your question, you can validate user input with:
/^(?!(?:0{6}|00-00-00))(?:\d{6}|\d\d-\d\d-\d\d)$/.test(inputString)
It will strictly match only input in the form XX-XX-XX or XXXXXX where X are digits, and will exclude 00-00-00, 000000 along with any other cases (e.g. XX-XXXX or XXXX-XX).
However, in my opinion, as stated in other comments, I think it is still better if you force user to either always enter the hyphen, or none at all. Being extra strict when dealing with anything related to money saves (unknown) troubles later.
Since any of the digits can be zero, but not all at once, you should treat the one case where they are all zero as a single, special case.
You are checking for two digits (\d{2}), then an optional hyphen (-?), then another two digits (\d{2}) and another optional hyphen (-?), before another two digits (\d{2}).
Putting this together gives \d{2}-?\d{2}-?\d{2}, but you can simplify this further:
(\d{2}-?){2}\d{2}
You then use the following pseudocode to match the format but not 000000 or 00-00-00:
if (string.match("/(\d{2}-?){2}\d{2}/") && !string.match("/(00-?){2}00/"))
//then it's a valid code, you could also use (0{2}-?){2}0{2} to check zeros
You may wish to add the string anchors ^ (start) and $ (end) to check the entire string.
Two quick questions:
What would be a RegEx string for three letters and two numbers with space before and after them (i.e. " LET 12 ")?
Would you happen to know any good RegEx resources/tools?
For a good resource, try this website and the program RegexBuddy. You may even be able to figure out the answer to your question yourself using these sites.
To start you off you want something like this:
/^[a-zA-Z]{3}\s+[0-9]{2}$/
But the exact details depend on your requirements. It's probably a better idea that you learn how to use regular expressions yourself and then write the regular expression instead of just copying the answers here. The small details make a big difference. Examples:
What is a "letter"? Just A-Z or also foreign letters? What about lower case?
What is a "number"? Just 0-9 or also foreign numerals? Only integers? Only positive integers? Can there be leading zeros?
Should there be a single space between the letters and numbers? Or any amount of any whitespace? Even none?
Do you want to search for this string in a larger text? Or match a line exactly?
etc..
The answers to these questions will change the regular expression. It would be much faster for you in the long run to learn how to create the regular expression than to completely specify your requirements and wait for other people to reply.
I forgot to mention that there will be a space before and after. How do I include that?
Again you need to consider the questions:
Do you mean just one space or any amount of spaces? Possibly not always a space but only sometimes?
Do you mean literally a space character or any whitespace characters?
My guess is:
/^\s+[a-zA-Z]{3}\s+[0-9]{2}\s+$/
/[a-z]{3} [0-9]{2}/i will match 3 letters followed by a whitespace character, and then 2 numbers. [a-z] is a character class containing the letters a through z, and the {3} means that you want exactly 3 members of that class. The space character matches a literal space (alternately, you could use \s, which is a "shorthand" character class that matches any whitespace character). The i at the end is a pattern modifier specifying that your pattern is case-insenstive.
If you want the entire string to only be that, you need to anchor it with ^ and $:
/^[a-z]{3} [0-9]{2}$/i
Regular expression resources:
http://www.regular-expressions.info - great tutorial with a lot of information
http://rexv.org/ - online regular expression tester that supports a variety of engines.
^([A-Za-z]{3}) ([0-9]{2})$ assuming one space between the letters/numbers, as in your example. This will capture the letters and numbers separately.
I use http://gskinner.com/RegExr/ - it allows you to build a regex and test it with your own text.
As you can probably tell from the wide variety of answers, RegEx is a complex subject with a wide variety of opinions and preferences, and often more than one way of doing things. Here's my preferred solution.
^[a-zA-Z]{3}\s*\d{2}$
I used [a-zA-Z] instead of \w because \w sometimes includes underscores.
The \s* is to allow zero or more spaces.
I try to use character classes wherever possible, which is why I went with \d.
\w{3}\s{1}\d{2}
And I like this site.
EDIT:[a-zA-Z]{3}\s{1}\d{2} - The \w supports numeric characters too.
try this regularexpression
[^"\r\n]{3,}
I am working on a php+javascript based project and have already made up a mockup page at :
my website
I knew how to use javascript or php to check whether a particular field of form is "empty"or not, that is, whether it contains alphanumerical characters other than whitepsace characters(for instance, space, tab and newline).
However, my normal apporach no longer works since the jquery plugin that I am using now relies on regex to validate the fields.
If you go to the third tab(3. Fill up Shipping Info and Make Payment), you can enter something into the Firstname field and it does the check automatically. Fine. However, if
you just simply put some space characters there and jump to the next field, well, it still feels okay for that, which is not correct since no one's first name is nothing!
The problem? At the back it has a regex like this :
"noSpecialCaracters":{
"regex":"/^[0-9a-zA-Z ]+$/",
"alertText":"* No special caracters allowed"},
This would not filter out empty characters.
I searched online and tried my best to make up another regex to match, I tried
"regex":"/^[^]+$/"
for matching non-empty characters, but that will not do...
Can anyone help me out? Many thanks in advance!
Try this for non-whitespace:
([^\s]*)
Example:
/([^\s])/.test(" A"); //TRUE
/([^\s])/.test(" "); //FALSE
function anyChar(str){
return /\S+/.test(str);
}
will return false if emty data
I'm using
/^\s*\S+.*/
which means
zero or more whitespace characters (\s*), followed by
one or more non-whitespace characters (\S+), followed by
anything at all, whitespace or not (.*).
This allows a single word or multiple words. I'm allowing whitespace at the beginning because I know how easy it is to miss a single space at the beginning and be really confused as to why your input isn't allowed :|
The Mozilla Developer Network has a great JavaScript regex page for reference.
You may want to try wrapping your expression in the following ^\s*(expression)\s*$. Then use the groups to find the "trimmed" matches. This eliminates only trailing or leading whitespace.
You can force the user to enter trimmed text or you can gracefully accept untrimmed input (better) as I find copying and pasting text often leaves some trailing or leading whitespace which the user may be unaware of.
To answer your question, the minimal regex is /\S/ which will match as long as there is at least one non-whitespace character.
However, you probably don't want someone to put in a first name of '12345' or '!!!', so it might be better to use /[a-z]/i as this regex will only match if there is at least one alphabetical character.
/^\s*[0-9a-zA-Z][0-9a-zA-Z ]*$/
that ensures that at least one character is not whitespace and is of one of the allowed characters.
You may also want to consider other characters like hyphen(-) or apostrophe(') that may also appear in names...
/^\s*[0-9a-zA-Z][0-9a-zA-Z '-]*$/