Python Regex - lowercase capitalization after

I am trying to check an uppercase letter that has a lowercase letter immediately following it. The trick is that there will be a bunch of garbage caps and a quantity in front. For instance:

AASKH317298DIUANFProgramming is fun 

as you can see, there are tons of things that we don’t need, right before the phrase we need, Programming is fun .

I am trying to use regex for this, taking each line and then replacing it with '' , since the original line does not need to be saved.

 re.sub(r'^[A-Z0-9]*', '', string) 

The problem with this code is that it leaves us with rogramming is fun , since P is uppercase.

As I will check, to make sure that if the next letter is lowercase, then I must leave this capital intact. ( P in Programming )

+6
source share
2 answers

Use a negative forecast ahead:

 re.sub(r'^[A-Z0-9]*(?![az])', '', string) 

This matches any uppercase character or number that is not followed by a lowercase character.

Demo:

 >>> import re >>> string = 'AASKH317298DIUANFProgramming is fun' >>> re.sub(r'^[A-Z0-9]*(?![az])', '', string) 'Programming is fun' 
+11
source

You can also use a match as follows:

 >>> import re >>> s = 'AASKH317298DIUANFProgramming is fun' >>> r = r'^.*([AZ][az].*)$' >>> m = re.match(r, s) >>> if m: ... print(m.group(1)) ... Programming is fun 
+3
source

All Articles