Regex to match only few letters and numbers - regex

How can I write a regex that matches letters ('R', 'L'), numbers and first character is always letter.
E.G.
I want regex to accept string like "R12L", "L1" that start with either 'R' or 'L' only.

I believe you want to match words that:
Start with any letter
contain numbers, 'R' and 'L'
Here is: \b[a-zA-Z][0-9RL]*\b
In case the first letter must be either 'R' or 'L', then this will be better:
`\b[RL][0-9RL]*\b`
Explanation:
\b is a word boundary, a zero length match
[RL] is a character class, it matches either R or L
[0-9] is a range within the character class, it matches anything between 0 and 9.
You can play with this demo.

Related

Regular Expressions - A word with only one capitalized letter and which doesn't contain numbers

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

Print strings with regex rules

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}$/

How to check if given username has at least one letter in ruby

I have this regex:
^(?=.*[a-zA-Z])(?=.*[0-9])
But this requires both letter and number. What I need to check if username has either full of letters like pratha or letters with numbers pratha123 or 123pratha but not just numbers 1234156
I couldn't adjust this regex to fit my needs. How can i adjust to work like this?
Note that, there is no sequence like it should start with letter or not. It can start with number or letter but it should have at least one letter. I just want to discard numbers-only. And username is just a-zA-Z0-9. So no Unicode chars.
Using Ruby 2.6.3
Demo: https://rubular.com/r/WT9VCAphYyEMIc
Update: I discovered that this pattern also allows spaces. It shouldn't.
As I understand you want to determine if the string has only letters and numbers and at least one letter. You could use the following regular expression:
r = /\A\p{Alnum}*\p{L}\p{Alnum}*\z/
This reads, "match a start-of-string anchor, followed by zero or more alphanumeric (Unicode) characters (letters or numbers), followed by a letter, followed by zero or more alphanumeric characters, followed by an end-of-string anchor".
"12abc34".match?(r) #=> true
"1234567".match?(r) #=> false
"=12abc3".match?(r) #=> false
"".match?(r) #=> false
Another way:
r = /\A(?=.*\p{L})\p{Alnum}*\z/
This reads, "match a start-of-string anchor, followed by a letter preceded by zero or more of characters, in a positive lookahead (which consumes no characters), followed by zero or more alphanumeric characters, followed by an end-of-string anchor".
At least one letter:
username.count("a-zA-Z") > 0
username is just a-zA-Z0-9, so no Unicode chars:
username.count("^a-zA-Z0-9") == 0
I am sure there is a more clever way to do this but that one does the job.
USERNAME_REGEX = /\A[a-z0-9]*[a-z]+[a-z0-9]*\z/i
if username.match USERNAME_REGEX
# It matches
else
# It does not match
end
The \A at the beginning and the \z at the end makes sure that the whole string matches the regex, otherwise it would just check if it contains it.
The trick is to make sure there is at least one letter in the middle and 0 or many letters or numbers before and after.
I assumes you wanted ascii characters but if you want UTF-8 instead, it should be easy to change.

Regex: 1 characters 8 numbers

I thought this regular expression would work:
preg_match("/^e[a-z]i\d{7}$/", $_POST['username'])
I'm trying to validate a user's login credentials to match the following format:
e00123456
The 'e' can be either e or E and the 8 numbers after the 'e' can be any number ranging from 0-9. But it has to be the letter 'e' followed by 8 numbers, no more, no less. What's wrong with my regular expression?
you're currently matching:
^: From the start
e: find an e
[a-z]: then one character which is in abcdefghijklmnopqrstuvwxzy
i: followed by an i
\d{7}: followed by seven digits
$: And only match if this is the last character.
You're not yet being case-insensitive.
You want to do:
/^e\d{8}$/i
You will probably need to escape the \. The /i sets case-insensitive mode in sed and Vim, you might need to set that differently if you're using PHP.
What's the point of [a-z]? That will match any lower case letter. Also, why do you have \d{7} if you want 8 digits? Try this:
^[eE]\d{8}$
The [eE] character class will match either an upper or lower case 'e'.
Use may use this in your regular expression validation
[eE]{1}\d{8}
[eE] means only 'e' or 'E' is allowed
{1} means only 1st letter
\d{8} means next 8 letters will be digits ..
Hope, this may help you .

How to regex a string - length 8, first character letter and remaining numeric

I am trying to create a RegEx to match a string with the following criterion
Length 8
First character must be a letter a-z or A-Z
The remaining 7 must be numeric 0-9
examples
a5554444
B9999999
c0999999
This is what I have so far
^[0-9]{8}$
What am I missing to check the first character? I tried
^[a-zA-Z][0-9]{8}$
but that's not working.
I think this is what you want:
^[a-zA-Z][0-9]{7}$
the {...} metacharacter only matches the most previous pattern which in your case is [0-9]. the regex interpretation is as follows:
start at the beginning of the string (^)
match any character a-z or A-Z ([a-zA-Z]) in the first spot only one time
match any character 0-9 starting at the second spot ([0-9])
the preceding pattern mentioned in step 3 of [0-9] must exist exactly 7 times ({7})
When you put {8} as per your original question, you'll assume a string length total of 9: the first character being alphabetic case insensitive and the remaining 8 characters being numeric.