python regex problem

Hello,

In a text I have to find a word with this pattern :

one lowercase character then three capitalized characters then one lowercase, three capitalized and one lowercase.

I tried this one : ‘[a-z][A-Z]+[A-Z]+[A-Z]+[a-z]+[A-Z]+[A-Z]+[A-Z][a-z]’
But that don’t work.

Can any give me a hint ?

Roelof

You seem to misunderstand the meaning of +. When you have understood, note that the + is greedy, it will swallow as many characters that match, so what you really want is:

[a-z][A-Z][A-Z][A-Z][a-z][A-Z][A-Z][A-Z][a-z]

or more succinctly

[a-z][A-Z]{3}[a-z][A-Z]{3}[a-z]

You might be interested in this site:

Python Regex Tool

oke,

Thank you.
It works but not perfect.

I try to solve this puzzle (re)
I get a hint that there are 10 “words” that match.
I get now only one.

So i have to puzzle how to solve that ?

Roelof

oke,

found the problem.
I have this code :


import re

challenge = open('challenge2.txt')
regel = challenge.read()
uitkomst = re.search('[a-z][A-Z]{3}[a-z][A-Z]{3}[a-z]', regel)
print uitkomst.group()

It’s only find the first occurence.

Anyone a tip how I can make it find and display everything

Wouldnt that the

regex.findall(string)

like it is to seen in the “Python Regex Tool” (Thanks for the Link, Keny-Yap :slight_smile: )

(Because here it finds all occurences of the matching words according to your pattern)

Hello,

Your right.

regex.findall did the trick of finding all the words.

Thanks all for the help.

Now trying to solve a pickle problem.

Roelof