i need regex to print strings by these rules:
letters a-f (lower case)
numbers 0-9
letter can't be after letter (forms like: 'aa' ,'bc', 'db')
string length will be exactly 20.
I have tried to play with some regex expressions but with no luck
You may use a negative preceding context for letter characters:
/^(?:[0-9]|(?<![a-f])[a-f]){20}$/
Related
I am new to RegExp. I have a sentence and I would like to pull out a word which satisfies the following -
It must contain only one capitalized letter
It must consist of only characters/letters without numbers
For instance -
"appLe", "warDrobe", "hUsh"
The words that do not fit - "sf_dsfsdF", "331ffsF", "Leopard1997", "mister_Ram" et cetera.
How would you resolve this problem?
The following regex should work:
will find words that have only one capital letter
will only find words with letters (no numbers or special characters)
will match the entire word
\b(?=[A-Z])[A-Z][a-z]*\b|\b(?=[a-z])[a-z]+[A-Z][a-z]*\b
Matches:
appLe
hUsh
Harry
suSan
I
Rejects
HarrY - has TWO capital letters
warDrobeD - has TWO capital letters
sf_dsfsdF - has SPECIAL characters
331ffsF - has NUMBERS
Leopd1997 - has NUMBERS
mistram - does not have a CAPITAL LETTER
See it in action here
Note:
If the capital letter is OPTIONAL- then you will need to add a ? after each [A-Z] like this:
\b(?=[A-Z])[A-Z]?[a-z]*\b|\b(?=[a-z])[a-z]+[A-Z]?[a-z]*\b
You can do this by using character sets ([a-z] & [A-Z]) with appropriate quantifiers (use ? for one or zero capitals), wrapped in () to capture, surrounded by word breaks \b.
If the capital is optional and can appear anywhere use:
/\b([a-z]*[A-Z]?[a-z]*)\b/ //will still match empty string check for length
If you always want one capital appearing anywhere use:
/\b([a-z]*[A-Z][a-z]*)\b/ // does not match empty string
If you always want one capital that must not be the first or last character use:
/\b([a-z]+[A-Z][a-z]+)\b/ // does not match empty string
Here is a working snippet demonstrating the second regex from above in JavaScript:
const exp = /\b([a-z]*[A-Z][a-z]*)\b/
const strings = ["appLe", "warDrobe", "hUsh", "sf_dsfsdF", "331ffsF", "Leopard1997", "mister_Ram", ""];
for (const str of strings) {
console.log(str, exp.test(str))
}
Regex101 is great for dev & testing!
RegExp:
/\b[a-z\-]*[A-Z][a-z\-]*\b/g
Demo:
RegEx101
Explanation
Segment
Description
\b[a-z\-]*
Find a point where a non-word is adjacent to a word ([A-Za-z0-9\-] or \w), then match zero or more lowercase letters and hyphens (note, the hyphen needs to be escaped (\-))
[A-Z]
Find a single uppercase letter
[a-z\-]*\b
Match zero or more lowercase letters and hyphens, then find a point where a non-word is adjacent to a word
I need a regex to return exactly three MATCHES (not groups) for some names input strings that can be formatted in any of the following ways:
Last First Middle
LAST First Middle
Last FirstMiddle
LAST FirstMiddle
LastFirst Middle
LASTFirst Middle
LastFirstMiddle
LASTFirstMiddle
So far, I have worked out this: ([A-Z]{2,})?([A-Z][a-z]*)
It correctly returns three MATCHES for all scenarios except "LASTFirstMiddle" and "LASTFirst Middle", when the last name is capitalized and concatenated with the first name.
It is important that the result produces three MATCHES, not groups.
Please could you suggest a Regex that could support this? Thank you.
You can use
[A-Z]{2,}(?=[A-Z][a-z]|\b)|[A-Z][a-z]*
See the regex demo. Details:
[A-Z]{2,}(?=[A-Z][a-z]|\b) - two or more uppercase ASCII letters followed with an uppercase ASCII letter and then a lowercase ASCII letter, or a word boundary
| - or
[A-Z][a-z]* - an uppercase ASCII letter and then zero or more lowercase ASCII letters.
I have written the following regEx to check for the below conditions
Combination of at-least one lowerCase a-z with Uppercase A-Z
Combination of at-least one lowerCase a-z with a digit 0-9
Combination of at-least one UpperCase A-Z with a digit 0-9
((?=.*[a-z])(?=.*[A-Z]))|((?=.*[a-z])(?=.*[0-9]))|((?=.*[A-Z])(?=.*[0-9]))
valid inputs are:-
aA
a1
A1
Is there any way to short/simplify this regex?
Would ([a-z]+[A-Z])|(([a-z]+|[A-Z]+)[0-9]) be what you're looking for?
If I understand correctly, the rules could be rephrased like:
Input must consist of only alphanumerical characters
Reject any input that only consists of digits
Reject any input that only consists of lowercase letters
Reject any input that only consists of uppercase letters
Your regular expression does not check for the start/end of the input, nor does it verify that all characters are alphanumerical (.* is too broad), so it would accept inputs that are invalid.
You could do it like this:
^(?!\d*$|[A-Z]*$|[a-z]*$)[a-zA-Z\d]*$
i want to match the strings which is listed below other than than that whatever the string is it should not match
rahul2803
albert1212
ra456
r1
only the above mentioned strings should match in the following group of data
rahul
2546rahul
456
rahul2803
albert1212
ra456
r1
rahulrenjan
r4ghyk
i tried with ([a-z]*[0-9]) but it's not working.
In regular expressions * means zero or more so your regex matches zero letters. If you want one or more use + (\d means digit).
^[a-zA-Z]+\d+$
Regular expressions are fun to solve once you get the hang of the syntax.
This one should be pretty straight:
Start with a letter. ^[a-z] (I am not taking the case of capital
letters here, if they are then ^[a-zA-Z] )
Have multiple letters/digits in between .*
End the string with a digit [0-9]$
Combine all 3 and you get:
^[a-z].*[0-9]$
So right now im trying to create a regex that takens in ID's. The ID is a string so it can have letters and numbers. However, i need to make an expression to see that it gets digits, it should not take in letters anymore and end the expression.
What i have:
[a-zA-Z]([a-zA-Z]|[0-9])*
Example:
"Bob23Dan"
Example answer:
1) "Bob23"
2) "Dan"
This will match a variable number of letters (atleast one) that ends with a variable number of numbers (optional)
[a-zA-Z]+[0-9]*
If you can tolerate there being letters after the digits in the original string, but you just don't want to match them, then I think you need this:
^[a-zA-Z]*[0-9]+
which will take in any number of letters from the start of the string (^), then at least one digit. It will fail to match if there are no digits, but pass if there are digits but no letters.
If you want to make sure there are no letters after the digits in the original string (the purpose of the regex is to test rather than to match), then append the end of line char ($) like this:
^[a-zA-Z]*[0-9]+$