Regular Expression for Uppercase Letters in a String

Regular expression for checking if capital letters are found consecutively in a string

Take a look at tchrist's answer, especially if you develop for the web or something more "international".

Oren Trutner's answer isn't quite right (see sample input of "RightHerE" which must be matched, but isn't).

Here is the correct solution:

(?!^.*[A-Z]{2,}.*$)^[A-Za-z]*$

Explained:

(?!^.*[A-Z]{2,}.*$)  // don't match the whole expression if there are two or more consecutive uppercase letters
^[A-Za-z]*$ // match uppercase and lowercase letters

/edit

The key for the solution is a negative lookahead. See: Lookahead and Lookbehind Zero-Length Assertions

regex for strings that are Uppercase letters with numbers of minlength 6, and Uppercase letters with numbers of minlength 6 and hyphen and only number

You can use

(?<![\dA-Z-])(?=[\dA-Z-]{6,})(?:[\d-]+[A-Z]|[A-Z-]+\d)[A-Z\d-]*|[0-9]{6,}

See the regex demo. Details:

  • (?<![\dA-Z-]) - immediately to the left, there should be no digit, uppercase letter or -
  • (?=[\dA-Z-]{6,}) - immediately to the right, there must be 6 or more digits, uppercase letters or -
  • (?:[\d-]+[A-Z]|[A-Z-]+\d)[A-Z\d-]* - one or more digits or - and then an uppercase letter or one or more uppercase letters or hyphens and then a digit and then zero or more uppercase letters, digits or hyphens
  • | - or
  • [0-9]{6,} - six or more digits.

How do I get a regex expression to contain only uppercase letters or numbers?

Starting the character class with [^ makes is a negated character class.

Using ([^0-9]|[^A-Z])+ matches any char except a digit (but does match A-Z), or any char except A-Z (but does match a digit).

This way it can match any character.

If you would turn it into [A-Z]([0-9]|[A-Z])+[A-Z] it still does not make it mandatory to match at least a single digit on the inside due to the alternation | and it can still match AAA for example.

You might use:

^[A-Z]+[0-9][A-Z0-9]*[A-Z]$

The pattern matches:

  • ^ Start of string
  • [A-Z]+ Match 1+ times A-Z
  • [0-9] Match a single digit
  • [A-Z0-9]* Optionally match either A-Z or 0-9
  • [A-Z] Match a single char A-Z
  • $ End of string

Regex demo

Regex for last capital letter in string

Update
'HelloMyNameIsNick'.replace(/([^ \n])([A-Z][^A-Z]*$)/gm, '$1 $2')

Test cases:

console.log(`
HelloMyNameIsNick
Hello
Hello
HELLO
HellO
HeLLo
Hello Test
HELLO TEST
hello test
HELLO 1$@(-.aa
HELLO 1$@(-.Aa
`.replace(/([^ \n])([A-Z][^A-Z]*$)/gm, '$1 $2'))

Regex - Count number of uppercase letters in word

You can use

re.findall(r'\b(?:[a-z]*[A-Z]){2}[a-zA-Z]*\b', text)

See the regex demo. Details:

  • \b - a word boundary
  • (?:[a-z]*[A-Z]){2} - two sequences of zero or more lowercase letters followed with an uppercase letter
  • [a-zA-Z]* - zero or more ASCII letters
  • \b - a word boundary

See the Python demo:

import re
text = "A VeRy LoNG SenTence Here"
print(re.findall(r'\b(?:[a-z]*[A-Z]){2}[a-zA-Z]*\b', text))
# => ['VeRy', 'LoNG', 'SenTence']

A fully Unicode-aware regex is possible with the PyPi regex library (install in your terminal/console with pip install regex):

import regex
text = "Да, ЭтО ОченЬ ДЛинное предложение."
print(regex.findall(r'\b(?:\p{Ll}*\p{Lu}){2}\p{L}*\b', text))
# => ['ЭтО', 'ОченЬ', 'ДЛинное']

See this Python demo.

Regular expression to match String has unique uppercase letters

Original Problem:

You may use this regex:

^(?:([A-Z])(?!.*\1)){3}$

RegEx Demo

RegEx Details:

  • ^: Start
  • (?:: Start non-capture group
    • ([A-Z]): Match an uppercase letter and capture in group #1
    • (?!.*\1): Negative lookahead to make sure there is no repeat of same letter anywhere ahead
  • ){3}: End non-capture group. {3} makes it match 3 of these
  • $: End

Updated Problem: Based on your updated problem you can use this regex to match 3 unique uppercase letters followed by 4 digit year from 1900 to 2019.

^((?:([A-Z])(?![A-Z]*\2)){3})(19\d{2}|20[01]\d)$
  • (19\d{2}|20[01]\d) matches 4 digit year from 1900 to 2019

RegEx Demo 2

Check if string is uppercase and numbers

Generally speaking, if you want to match whole string using regex, you will usually end up using ^ and $ to describe the start and end of string.

Also, just [A-Z_0-9] matches a single character.

Assuming you don't allow whitespaces, ^[A-Z_0-9]*$ would be the regex you're looking for.

Regular Expression for UpperCase Letters In A String

  1. You didn't call matches or find on the matcher. It hasn't done any work.

  2. getGroupCount is the wrong method to call. Your regex has no capture groups, and even if it did, it wouldn't give you the character count.

You should be using find, but with a different regex, one without anchors. I would also advise using the proper Unicode character class: "\\p{Lu}+". Use this in a while (m.find()) loop, and accumulate the total number of characters obtained from m.group(0).length() at each step.

Regex, two uppercase characters in a string

You can use the simple regex

[A-Z].*[A-Z]

which matches an upper case letter, followed by any number of anything (except linefeed), and another uppercase letter.

If you need it to allow linefeeds between the letters, you'll have to set the single line flag. If you're using JavaScript (U should always include flavor/language-tag when asking regex-related questions), it doesn't have that possibility. Then the solution suggested by Wiktor S in a comment to another answer should work.

Regular expression to check 3 uppercase letters anywhere

I don't think you need to use lookahead for this.
Using the same idea as anubhava went with, but putting it in the body of the regexp instead should also work.

^(.*[A-Z]){3}.*$

As you noted in your question .*[A-Z] checks for a single uppercase letter. So to look for 3 of them we just repeat that 3 times. (foo){3} is a regexp shorthand for (foo)(foo)(foo).

In your regexp you started the group with ?=, meaning that this was a lookahead. This essentially asks the matcher if there is a match on the assertion starting on the subgroup you are currently on. However, since this is the only condition we are testing for we might as well make it our primary expression and avoid the lookahead.



Related Topics



Leave a reply



Submit