How to write a regular expression for validating a organisation name which allows Alphanumeric as the starting characters and only special characters like ., -, # and &.
I tried but it's not working
/^[a-z]|\d?[a-zA-Z0-9]?[a-zA-Z0-9\s&#.]+$
Some Valid Names
Hercules.Cycle
Herbal & Product
Welcome # 123
Invalid Names
&Hercules
Colgate!()
.Youtube
#Incule
Is that what you want?
^[A-Z]([a-zA-Z0-9]|[- #\.#&!])*$
You guys can use this below validation ,
As per our requirement from client, "Company Name" can have only 'single space' in between words along with few permitted special characters in it.
/^[a-zA-Z0-9-#.{}#&!()]+(\s[a-zA-Z0-9-#{}.#&!()]+)+(\s[a-zA-Z-#.#&!()]+)?$/
Output:-
Correct/Positive Response.
These below mentioned outputs are allowed..
#arshaa Technologies (m91) HYD
#9Arshaa Technologies (HYD) IND
#AT&T {IND} HYD
#Apple.India {HYD} (INDIA)
Negative/Incorrect response.
These below mentioned few outputs will not be allowed, according to above mentioned RegEx. As they contain multiple spaces or Special Characters not in RegEx.
#arshaa _Technologies (m91) HYD
#9Arshaa --Technologies (HYD) IND
#AT&T {IND} HYD.
#Apple. -^India {HYD} $(INDIA)
Note: Please feel free to update your answers in comments section for further modification of this RegEx.
I've made and tested the above regular expression and it solves all what you mentioned:
/^[.#&]?[a-zA-Z0-9 ]+[ !.#&()]?[ a-zA-Z0-9!()]+/
Below all checks off.
Some Valid Names
Hercules.Cycle
Herbal & Product
Welcome # 123
Invalid Names
&Hercules
Colgate!()
.Youtube
#Incule
Try this pattern:
^\w[\w.\-#&\s]*$
or
^[a-zA-Z0-9][a-zA-Z0-9\.\-#&\s]*$
^[A-Z]([a-zA-Z0-9.-_,]|[- #.#&!])*$
This would allow certain special characters after the first word
Sample Names:
1. Hoyt, Gilman & Esp
2. Y 105 Kgfy
Related
I need some help to find a regex expression to extract user names from these emails:
(Regex newbie here)
john.stewartcompany1#example.com
bruce.williamscompany1#example.com
richard.weiss#example.com
julia.palermocompany2#example.com
edward.philipscompany3#example.com
As you can see from the emails, almost all of them have the company name following the name. (company1, company2, company3)
But some emails have no company inserted. (See richard.weiss)
All of them will have #example.com
So I need to extract only the names, without the company, like this:
john.stewart
bruce.williams
richard.weiss
julia.palermo
edward.philips
I've come up with this pattern so far:
/(.+)(?=#example.com)/g
This only solves half of the problem, as it keeps the company name in the names.
john.stewartcompany1
bruce.williamscompany1
richard.weiss
julia.palermocompany2
edward.philipscompany3
I still need to remove the company names from the user names.
Is there a way to accomplish this with a single regex pattern?
Any help appreciated.
PS:
Thanks for the replies. I forgot to mention...
The company names are limited.
We can safely assume from my example that there will be only
company1, company2 and company3.
Thanks.
You can use
^.*?(?=(?:company1|company2|company3)?#)
See the regex demo.
Details:
^ - start of string
.*? - any zero or more chars other than line break chars as few as possible
(?=(?:company1|company2|company3)?#) - a positive lookahead that requires the following subpatterns to match immediately to the right of the current location:
(?:company1|company2|company3)? - an optional company1, company2 or company3 char sequence
# - a # char.
I have to set some rules on not accepting wrong url for my project. I am using regex for this.
My Url is "http ://some/resource/location".
This url should not allow space in beginning or middle or in end.
For example these spaces are invalid:
"https ://some/(space here in middle) resource/location"
"https ://some/resource/location (space in end)"
"(space in starting) https ://some/resource/location"
"https ://(space here) some/resource/location"
Also these scenario's are invalid.
"httpshttp ://some/resource/location"
"https ://some/resource/location,https ://some/resource/location"
Currently I am using a regex
var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*#)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%#!\-\/]))?/;
This regex accepts all those invalid scenarios. I am unable to find the correct matching regex which will accept only if the url is valid. Can anyone help me out on this?
We need to validate n number of scenarios for URL validation. If your particular about your given pattern then above regex expression from other answer looks good.
Or
If you want to take care of all the URL validation scenarios please refer In search of the perfect URL validation regex
/(ftp|http|https){1}:\/\/(?:.(?! ))+$/
is this regex OK ?
use this
^\?([\w-]+(=[\w-]*)?(&[\w-]+(=[\w-]*)?)*)?$
See live demo
This considers each "pair" as a key followed by an optional value (which maybe blank), and has a first pair, followed by an optional & then another pair,and the whole expression (except for the leading?) is optional. Doing it this way prevents matching ?&abc=def
Also note that hyphen doesn't need escaping when last in the character class, allowing a slight simplification.
You seem to want to allow hyphens anywhere in keys or values. If keys need to be hyphen free:
^\?(\w+(=[\w-]*)?(&\w+(=[\w-]*)?)*)?$
I'm trying to create a good regular expression for people's Surname.
It should be valid if a Surname is:
abcd
abcd'efg
abcd-efg
abcd, .efg
etc...
I also need to test if symbols do not repeat... so for example:
abcd''efg
abcd-',
Are invalid but the one:
abcd, .efg
Can be valid.
At the moment I just created this:
^[a-z .',-_]+$
And now I'm trying to check for all the double symbols but I cannot go ahead successfully.
It's a bad idea. There is no international list of allowed characters that people could use in their names. Some surnames even contain Unicode symbols — it will not be possible to write a regex that would perfectly validate all of them correctly. Even if you can come up with a regex, it might be too generic that it wouldn't be effective.
Read this article for why you shouldn't be doing this: Falsehoods Programmers Believe About Names
If after reading this insightful post by Amal Murali and you still want to do this with a regex, please see this:
/^(?![^'\-_\n]*['\-_][^'\-_\n]*['\-_])[a-z .',-_]+$/m
View a regex demo!
I am try to validate a form to make the user enter their full name like the following..
First, Last
The user must have some string of alphabetic only chars, then a comma, then a space, then a last name, which can again be any string of chars.
This is my current regex..
var alphaExp = /^[a-zA-Z,+ a-zA-Z]+$/;
However, it lets the user submit something as simple as john. I want it to force them to submit something such as john, smith.
What you are doing is creating a character class ([]) that matches any of the following characters: a-zA-Z,+. This allows john to match the whole regex.
This is what you want:
/^[a-zA-Z]+, [a-zA-Z]+$/
However, I would like to advise you that making assumptions about names is a little wrong. What if some guy is named John O'Connor? Or Esteban Pérez? Or just Ng (only a first name)?
"Code that believes someone’s name can only contain certain characters is stupid, offensive, and wrong" - tchrist
Sure, you don't want to let people to enter just gibberish, but leave an option for users to enter something that doesn't necessarily fit your idea of correctness, but is nonetheless correct.
That's not how character sets work:
/^[a-zA-Z]+, [a-zA-Z]+$/
Things to consider:
Any validation you do on the client can be bypassed
Some people may have names with accented letters
Some cultures don't use just two names
^[a-zA-Z]+, [a-zA-Z]+$
Should work, however, do you want to prevent '.'? As in J. Hooker? And more words, like 'Jan van Hoogstrum'? Note also that you are preventing any accented characters being used. An option (although it allows underscores) is to use \w:
^(\w+ )$
Which would give you 'name name name...'. 'First, last' is not a common way to enter your name, so you'll frustrate a lot of users that way.
The correct regexp for allowing only the first letter to be capital would be:
/^[A-Z][a-z]+, [A-Z][a-z]+$/
I need to split a string like the one below, based on space as the delimiter. But any space within a quote should be preserved. There are two cases which needs to work
Case 1
research library "not available" author:"Bernard Shaw"
to
research
library
"not available"
author:"Bernard Shaw"
Case 2
research library "not available" author:Bernard
to
research
library
"not available"
author:Bernard
I am trying to do this with Javascript and regular expression.
var splitArray = query_string.match(/([^\s]*\"[^\"]+\")|\w+/g);
Case 1 works as required but Case 2 produces the result as below
research
library
"not available"
author
Bernard
I need both the cases to work with one Regex. Any ideas appreciated.
[^"\s]+(?:"[^"]+")?|"[^"]+"
Explanation:
[^"\s]+ # One or more non-space/non-quote characters
(?:"[^"]+")? # optionally followed by a quoted string
| # or
"[^"]+" # just a quoted string.
Assuming that there are no escaped quotes within quoted strings.
([^\s]*\"[^\"]+\")|\w+:?
I've tested this regex here: rubular
update:
you may want to include some more punctuation marks like ; , . ? !
e.g. research library! "not available" author:"Bernard Shaw" test1, test2; test2!
([^\s]*\"[^\"]+\")|\w+[:;\.,\?!]?
This works, at least for your two cases:
((?:[^\s]*\"[^\"]+\")|[\w:]+)
see here