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

Time:12-12

I am new to RegExp. I have a sentence and I would like to pull out a word which satisfies the following -

  1. It must contain only one capitalized letter
  2. 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?

CodePudding user response:

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!

CodePudding user response:

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

CodePudding user response:

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
  • Related