2

I am using python's re.findall method to find occurrence of certain string value in Input string. e.g. From search in 'ABCdef' string, I have two search requirements.

  1. Find string starting from Single Capital letter.
  2. After 1 find string that contains all capital letter.

e.g. input string and expected output will be:

  • 'USA' -- output: ['USA']
  • 'BObama' -- output: ['B', 'Obama']
  • 'Institute20CSE' -- output: ['Institute', '20', 'CSE']

So My expectation from

>>> matched_value_list = re.findall ( '[A-Z][a-z]+|[A-Z]+' , 'ABCdef' )

is to return ['AB', 'Cdef'].

But which does Not seems to be happening. What I get is ['ABC'] as return value, which matches later part of regex with full string.

So Is there any way we can ignore found matches. So that once 'Cdef' is matched with '[A-Z][a-z]+'. second part of regex (i.e. '[A-Z]+') only matches with remaining string 'AB'?

6
  • your question is unclear..and why do you need to do that..
    – Anirudha
    Feb 9, 2013 at 12:57
  • 1
    Can you detail what you would expect if your string was 'CdefAB'
    – sotapme
    Feb 9, 2013 at 13:44
  • @sotapme: It should be ['Cdef' , 'AB'].
    – lalit
    Feb 9, 2013 at 14:43
  • That's what I suspected, the good answers below don't deal with that case. I suggest you change your question to reflect that requirement. Likewise is it only for a pair of words ? What would you expect for 'ABXYZCdefXYZAbc' and 'MumABXYZCdefXYZAbc'
    – sotapme
    Feb 9, 2013 at 14:47
  • @Some1.Kill.The.DJ: I have few strings. Each of which contains few words without space separation (But each word starts with either Capital letter or number or it can be group of capital letters). I am trying to divide them into list of words by detecting where to separate. e.g. strings 'ANiceRoad', 'USA', 'BObama', 'Institute20CSE' etc.
    – lalit
    Feb 9, 2013 at 14:56

2 Answers 2

5

First you need to match AB, which is followed by an Uppercase alphabet and then a lowercase alphabet. or is at the end of the string. For that you can use look-ahead.

Then you need to match an Uppercase alphabet C, followed by multiple lowercase alphabets def.

So, you can use this pattern:

>>> s = "ABCdef"
>>> re.findall("([A-Z]+(?=[A-Z][a-z]|$)|[A-Z][a-z]+)", s)
['AB', 'Cdef']

>>> re.findall("([A-Z]+(?=[A-Z][a-z]|$)|[A-Z][a-z]+)", 'MumABXYZCdefXYZAbc')
['Mum', 'ABXYZ', 'Cdef', 'XYZ', 'Abc']

As pointed out in comment by @sotapme, you can also modify the above regex to: -

"([A-Z]+(?=[A-Z]|$)|[A-Z][a-z]+|\d+)"

Added \d+ since you also want to match digit as in one of your example. Also, he removed [a-z] part from the first part of look-ahead. That works because, + quantifier on the [A-Z] outside is greedy by default, so, it will automatically match maximum string, and will stop only before the last upper case alphabet.

4
  • Hi Rohit, Thanks alot for reply. But above solution might NOT work in case string is 'CdefAB' as mentioned by @sotapme above. In that case I won't get 'AB' in response list. But it will be ['Cdef']. Is there any way to make it such that It ignore already matched strings and work on rest?
    – lalit
    Feb 9, 2013 at 14:47
  • @lalit. Yeah updated the answer. Just add one $ in the first part of regex.
    – Rohit Jain
    Feb 9, 2013 at 14:56
  • I found during my tests of trying to understand your solution that "([A-Z]+(?=[A-Z]|$)|[A-Z][a-z]+)" worked ie. removed middle [a-z]
    – sotapme
    Feb 9, 2013 at 15:34
  • @sotapme. Yeah you are right. I overlooked that part. Since + quantifier is greedy by default, it will automatically match maximum possible string as possible. Nice catch :)
    – Rohit Jain
    Feb 9, 2013 at 15:36
1

You can use this regex

[A-Z][a-zA-Z]*?(?=[A-Z][a-z]|[^a-zA-Z]|$)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.