Regex statement for checking three uppercase letters?

I need a regex expression that will check for three uppercase letters in a string.

For example, it should match: ABC, aABC, abcABC

But this should not match: AaBbBc, ABCDE

At the moment, this is my statement:

'[^A-Z]*[A-Z]{3}[^A-Z]*'

But this corresponds to ABCDE, what am I doing wrong?

Thanks in advance.

+3
source share
5 answers

Regex

(?<![A-Z])[A-Z]{3}(?![A-Z])

Description

I indicated a negative Lookbehind and a negative Lookahead before and after the average regular expression for 3 capitals per line, respectively.

This is a better option than using a negative character class, because it will match successfully even if there are no characters in the left or right side of the string.

Online demo

DEMO


Python, , ,
:

re.match:

>>> import re
>>> p = re.compile(r'(?<![A-Z])[A-Z]{3}(?![A-Z])')
>>> s = '''ABC
... aABC
... abcABCabcABCDabcABCDEDEDEDa
... ABCDE'''
>>> result = p.match(s)
>>> result.group()
'ABC'

re.search:

>>> import re
>>> p = re.compile(r'(?<![A-Z])[A-Z]{3}(?![A-Z])')
>>> s = 'ABcABCde'
>>> p.search(s).group()
'ABC'
+5

[^A-Z]* " , 0." ABCDE . , A "0 ", BCD, E, "0 ".

, , , :

  • " " , .
  • " ".
  • " " .

, , 1. , 1.

:

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

, ^ , ^ . $ .

, :

regexp = /(^|[^A-Z])[A-Z]{3}([^A-Z]|$)/
'ABC'.match(regexp)    # returns a match
'aABC'.match(regexp)   # returns a match
'abcABC.match(regexp)  # returns a match
'AaBbBc'.match(regexp) # returns nil
'ABCDE'.match(regexp)  # returns nil
+3

, , , , ( , , ). , :

[^A-Z]*[A-Z]{3}[^A-Z]*

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

import re
theString = "ABCDE"
pattern = re.compile(r"([^A-Z]*)([A-Z]{3})([^A-Z]*)")
result = pattern.search(theString)

if result:
    print("Matched string: {" + result.group(0) + "}")
    print("Sub match 1: {" + result.group(1) + "} 2. {" + result.group(2) + "} 3. {" + result.group(3) + "}")
else:
    print("No match")

Matched string: {ABC}
Sub match 1: {} 2. {ABC} 3. {}

ideone

, ? [^A-Z]* "", , .

, , , , - :

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

, , (|^ OR |$ OR ). script , ABCDE, . abcABC, :

import re
theString = "abcABC"
pattern = re.compile(r"([^A-Z]|^)([A-Z]{3})([^A-Z]|$)")
result = pattern.search(theString)

if result:
    print("Matched string: {" + result.group(0) + "}")
    print("Sub match 1: {" + result.group(1) + "} 2. {" + result.group(2) + "} 3. {" + result.group(3) + "}")

Matched string: {cABC}
Sub match 1: {c} 2. {ABC} 3. {}

[^A-Z] ( , ) , , 3 , .


, , , result.group(2) .

, ...

(?:[^A-Z]|^)([A-Z]{3})(?:[^A-Z]|$)

result.group(1), 3

, ( ), . , :

(?<![A-Z])[A-Z]{3}(?![A-Z])

(?<! ... ) lookbehind , . , [A-Z], .

(?! ... ) , (). , [A-Z], . .group() :

import re
theString = "abcABC"
pattern = re.compile(r"(?<![A-Z])[A-Z]{3}(?![A-Z])")
result = pattern.search(theString)

if result:
    print("Matched string: {" + result.group() + "}")

ideone

, :)

+2

:

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

:

  • ^, $, .
  • (?:.*[^A-Z])?, , ( ).
0

,

'[^A-Z]*[A-Z]{3}[^A-Z]*'

^ i.e [], , , A-Z ( ) , . , , ,

[A-Z] {3} ,

[^ A-Z] * , .

"[A-Z] {3}", .

ABCde abCDE aBCDe ABCDE abcDE ABcDE AaBcCc

. Perl

#!/usr/bin/perl
use strict;
use warnings;

my @arr = qw(AaBsCc abCDE ABCDE AbcDE abCDE ABC aABC abcABC);

foreach my $string(@arr){
  if($string =~ m/[A-Z]{3}/){
    print "Matched $string\n";
  }
  else {
    print "Didn't match $string \n";
  }
}

:

Didn't match AaBsCc
Matched abCDE
Matched ABCDE
Didn't match AbcDE
Matched abCDE
Matched ABC
Matched aABC
Matched abcABC
0

All Articles