Rukki Odds
Rukki Odds

Reputation: 43

Regex divide with upper-case

I would like to replace strings like 'HDMWhoSomeThing' to 'HDM Who Some Thing' with regex.

So I would like to extract words which starts with an upper-case letter or consist of upper-case letters only. Notice that in the string 'HDMWho' the last upper-case letter is in the fact the first letter of the word Who - and should not be included in the word HDM.

What is the correct regex to achieve this goal? I have tried many regex' similar to [A-Z][a-z]+ but without success. The [A-Z][a-z]+ gives me 'Who Some Thing' - without 'HDM' of course.

Any ideas? Thanks, Rukki

Upvotes: 4

Views: 2606

Answers (5)

Greg Bacon
Greg Bacon

Reputation: 139521

#! /usr/bin/env python

import re
from collections import deque

pattern = r'([A-Z]{2,}(?=[A-Z]|$)|[A-Z](?=[a-z]|$))'
chunks = deque(re.split(pattern, 'HDMWhoSomeMONKEYThingXYZ'))

result = []
while len(chunks):
  buf = chunks.popleft()
  if len(buf) == 0:
    continue
  if re.match(r'^[A-Z]$', buf) and len(chunks):
    buf += chunks.popleft()
  result.append(buf)

print ' '.join(result)

Output:

HDM Who Some MONKEY Thing XYZ

Judging by lines of code, this task is a much more natural fit with re.findall:

pattern = r'([A-Z]{2,}(?=[A-Z]|$)|[A-Z][a-z]*)'
print ' '.join(re.findall(pattern, 'HDMWhoSomeMONKEYThingX'))

Output:

HDM Who Some MONKEY Thing X

Upvotes: 2

mtmk
mtmk

Reputation: 6316

May be '[A-Z]*?[A-Z][a-z]+'?

Edit: This seems to work: [A-Z]{2,}(?![a-z])|[A-Z][a-z]+

import re

def find_stuff(str):
  p = re.compile(r'[A-Z]{2,}(?![a-z])|[A-Z][a-z]+')
  m = p.findall(str)
  result = ''
  for x in m:
    result += x + ' '
  print result

find_stuff('HDMWhoSomeThing')
find_stuff('SomeHDMWhoThing')

Prints out:

HDM Who Some Thing

Some HDM Who Thing

Upvotes: 1

makapuf
makapuf

Reputation: 1400

one liner :

' '.join(a or b for a,b in re.findall('([A-Z][a-z]+)|(?:([A-Z]*)(?=[A-Z]))',s))

using regexp

([A-Z][a-z]+)|(?:([A-Z]*)(?=[A-Z]))

Upvotes: 2

Johrn
Johrn

Reputation: 1440

So 'words' in this case are:

  1. Any number of uppercase letters - unless the last uppercase letter is followed by a lowercase letter.
  2. One uppercase letter followed by any number of lowercase letters.

so try:

([A-Z]+(?![a-z])|[A-Z][a-z]*)

The first alternation includes a negative lookahead (?![a-z]), which handles the boundary between an all-caps word and an initial caps word.

Upvotes: 1

Gumbo
Gumbo

Reputation: 655299

Try to split with this regular expression:

/(?=[A-Z][a-z])/

And if your regular expression engine does not support splitting empty matches, try this regular expression to put spaces between the words:

/([A-Z])(?![A-Z])/

Replace it with " $1" (space plus match of the first group). Then you can split at the space.

Upvotes: 2

Related Questions