I am trying to build a regex expression for some validation. I want to check if a string is a combination of atleast one alphabet and one integer. For this i have tried this ^(?=.*[\w][\d]).+ I don't understand regex much. This expression checks for both aplhabet and number in a string but it wants the string to have an alphabet at the start. Instead i just want to check if both alphabet and number are present in a string irrespective of the number and order of occurence. Also the alphabet can be both capital or small so i guess the word checking will be case insensitive. The string might contain special characters along with word and digit in any combination and order but any space should be discarded. Can anyone help?
You'll have to use two lookaheads:
/(?=.*[a-z])(?=.*[0-9])/i
Blender's answer is correct, however I would recommend going for a regex that is easier to understand.
What you're looking for is really one of two scenarios: a string of characters that includes a letter first then a number sometime afterwards or the reverse.
The first scenario would then be: /.*[a-zA-Z].*[0-9].*/.
The second scenario would be: /.*[0-9].*[a-zA-Z].*/.
You can then combine these into one statement:
/(.*[a-zA-Z].*[0-9].*)|(.*[0-9].*[a-zA-Z].*)/
This can be simplified further but I hope this gives you some idea of how to approach regex problems like this.
Related
I am trying to use regex to check if the user input is in the correct format xx-xx (input only accepts numbers, does not accept alphanumeric characters)
I tried: /[1-9]{1,}\-[1-9]{1,}/ but when entering alphabetic characters still pass this test.
Can you guys help me. Thank.
Your regex is just fine, you just need to add positional asserts, "^" for the start of the string and "$" for the end of the string:
/^[1-9]{2}\-[1-9]{2}$/
It is better to put "{2}" if you only want xx-xx
/\d{2}-\d{2}/
Worked for me.
Breakdown:
\d checks for a digit character, i.e. 0-9.
{2} checks for two digits next to each other specifically.
- just checks for the hyphen character.
On the other hand, you can have several cases depending on what you are looking for precisely.
In case you agree that an xx-xx value can be 00-00 and any other value, you should use this regex instead:
/^\d{2}-\d{2}$/
In case you accept that an xx-xx value is never 00-00 but can be 01-03 and any other value, but never 00-00, you should use this regex (a bit long, sorry, but does the job perfectly):
/^[1-9](?=[0-9])[0-9]-[1-9](?=[0-9])[0-9]$|^[0-9](?=[1-9])[1-9]-[0-9](?=[1-9])[1-9]$/
Enjoy !
I want to match all valid prefixes of substitute followed by other characters, so that
sub/abc/def matches the sub part.
substitute/abc/def matches the substitute part.
subt/abc/def either doesn't match or only matches the sub part, not the t.
My current Regex is /^s(u(b(s(t(i(t(u(te?)?)?)?)?)?)?)?)?/, which works, however this seems a bit verbose.
Is there any better (as in, less verbose) way to do this?
This would do like the same as you mentioned in your question.
^s(?:ubstitute|ubstitut|ubstitu|ubstit|ubsti|ubst|ubs|ub|u)?
The above regex will always try to match the large possible word. So at first it checks for substitute, if it finds any then it will do matching else it jumps to next pattern ie, substitut , likewise it goes on upto u.
DEMO 1 DEMO 2
you could use a two-step regex
find first word of subject by using this simple pattern ^(\w+)
use the extracted word from step 1 as your regex pattern e.g. ^subs against the word substitute
I know how to do a regex to validate if it's just letter number without no white spaces:
/^[0-9a-zA-Z]+$/
but how do I add to this regex also such that it cannot contain just numbers, so for example this is not valid:
08128912382
Any ideas?
"Must contain only letters and numbers and at least one letter" is equivalent to "must contain a letter surrounded by numbers or letters":
/^[0-9a-zA-Z]*[a-zA-Z][0-9a-zA-Z]*$/
I would like to add that this answer shows a way you can think about the problem so writing the regexp is simpler. It is not meant to be the best solution to the problem. I just took what you had and gave it a nudge in the right direction.
With several more nudges, you end up with other different answers (posted by ZER0, Tomalak and OGHaza respectively) :
You could notice that if there is a letter in the first or last group, the middle part is satisfied. In other words, since you have the middle part, you don't need to allow letters in the first or last part (but not both!):
/^[0-9]*[a-zA-Z][0-9a-zA-Z]*$/ - some numbers, followed by a letter, followed by some more numbers and letters
/^[0-9a-zA-Z]*[a-zA-Z][0-9]*$/ - equivalent if you read from the end
Knowing about lookaheads you can assert that there is at least one letter in the string:
/^(?=.*[a-z])/ - matches the start of any string that contains at least 1 letter
Or the other way around, as you expressed it, assert that there aren't only numbers in the string:
/^(?!\d+$)/ - matches the start of any string which doesn't contain just digits
The 2nd and 3rd solutions should also be combined with your original regexp that validates that the string contains only the characters you want it to (letters and numbers)
I for one am particularly fond of the 2nd solution which is i believe the fastest of all attempted so far.
A look-ahead can do it:
/^(?=.*[a-z])[0-9a-z]+$/i
I think the most elegant solution is a negative lookahead to check it's not only numbers
/^(?!\d+$)[0-9a-zA-Z]+$/
RegExr Example
So basically you need at that at least one letter is in the string. In that case you can just check the presence of one or more letter, preceded maybe by one or more numbers, and maybe followed by both:
/^[0-9]*[a-z][0-9a-z]*$/i
Notice that it will returns true if you test against string like "A" for instance, because in this case all the numbers are considered optional.
I was just looking for a regex expression to check and see if both numbers and letters exist.
Just to clarify the query, the regex is going to be written in javascript and used to validate an address.
I would use a regular expression which matches any letter followed by any digit (with any possible characters in between) or digit then letter (with anything in between):
var hasNumbersAndLetters = function(str) {
var regex = /(?:[A-Za-z].*?\d|\d.*?[A-Za-z])/;
return !!str.match(regex);
};
Much easier to run two checks.
/\pL/ && /\pN/
To do both checks in one pattern, you need something like
/\pL.*\pN|\pN.*\pL/s
Languages supporting zero-width lookaheads can eliminate the redundancy:
/^(?=.*\pL/)(?=.*\pN/)/s ( or /^(?=.*\pL/).*\pN/s )
But it's harder to read.
Pardon me for not using JS's match function, but the question is really about regular expressions, and I'm not familiar with JS's match function.
if it is a single word you are matching without spaces, with both numbers and letters, it can be assumed they touch somewhere - so if it matches letter then number or number then letter we have a match - so:
([a-zA-Z][0-9]|[0-9][a-zA-Z])
Edit: where there may be spaces then you can use lookahead assertions like this
([a-zA-Z](?=.*[0-9])|[0-9](?=.*[a-zA-Z]))
Saw you wanted to validate an address
Removed by regex answer as javascript has no Unicode support, except for matching single characters http://www.regular-expressions.info/unicode.html
This should do it:
^[\w]*[^\W_][\w]*$
I've got the following sequence I'm attempting to detect...#hl=b&xhr=a where b is equal to anything and a is equal to anything.
I've got the following.. but it doesn't appear to be working... (#hl=.+&xhr=) Does anyone know why?
I'm using javascript and values a and b are letters of the alphabet.
(#hl=.+&xhr=.+), you missed the second .+. Depending on your regex engine, you should also see their escaping rules, often the braces or the + have to be escaped. If you just want to match a whole string, the braces are not needed anyway, btw.
You'll need to be more specific to get a better answer:
what programming language are you using RegEx in?
what values can a and b have? Anything implies that newlines are included, which . doesn't match
do you want to get the values of a and b?
Now that that's all been said, lets move onto a regex with some assumptions:
/#h1=(.+)&xhr=(.+)/
This will match a string #h1=a&xhr=b and select the a and b values from the string. It will be greedy, so if there are key-value pairs in the pseudo-URL (I assume it's a url encoded string as a hashtag) they will be matched in b.
#h1=a&xhr=b&foo=bar
the second selection will match b&foo=bar.
The regex also assumes #h1= comes before &xhr=.
Assuming #, & and = are special characters, how about this regular expression:
#h1=([^#&=]+)&xhr=([^#&=]+)
Are you sure your key/value pairs (?) are always in this order without anything in between?