5

My question is similar to this one, but with some modifications. First off I need to use python and regex. My string is: 'Four score and seven years ago.' and I want to split it by every 6th character, but in addition at the end if the characters do not divide by 6, I want to return blank spaces.

I want to be able to input: 'Four score and seven years ago.'

And ideally it should output: ['Four s', 'core a', 'nd sev', 'en yea', 'rs ago', '. ']

The closest I can get is this attempt, which ignores my period and does not give me the blank spaces

re.findall('.{%s}'%6,'Four score and seven years ago.') #split into strings
['Four s', 'core a', 'nd sev', 'en yea', 'rs ago']
Community
  • 1
  • 1
Bobe Kryant
  • 2,050
  • 4
  • 19
  • 32

3 Answers3

4

This is easy to do without regular expressions:

>>> s = 'Four score and seven years ago.'
>>> ss = s + 5*' '; [ss[i:i+6] for i in range(0, len(s) - 1, 6)]
['Four s', 'core a', 'nd sev', 'en yea', 'rs ago', '.     ']

This provides the blank spaces at the end that you asked for.

Alternatively, if you must use regular expressions:

>>> import re
>>> re.findall('.{6}', ss)
['Four s', 'core a', 'nd sev', 'en yea', 'rs ago', '.     ']

The key in both cases is creating the string ss which has enough blank space at the end.

John1024
  • 109,961
  • 14
  • 137
  • 171
  • I think you only want to add 5 extra spaces (`ss = s + 5*' '`). Six spaces works fine w/ the non-regex example; however, with the regex example, you get a final element consisting of 6 spaces if your original string length is a multiple of 6. – Mike Covington Nov 29 '15 at 02:19
  • 1
    @MikeCovington Very good! Thanks. I updated the answer to handle strings with lengths that are an even multiple of 6. – John1024 Nov 29 '15 at 03:22
3

The reason you aren't getting the final element containing a period is that your string is not a multiple of 6. Therefore, you need to change your regex to match 1 to 6 characters at a time:

>>> re.findall('.{1,6}','Four score and seven years ago.')
['Four s', 'core a', 'nd sev', 'en yea', 'rs ago', '.']

In order to get the desired padding of your final element, simply use this:

>>> [match.ljust(6, ' ') for match in re.findall('.{1,6}','Four score and seven years ago.')]
['Four s', 'core a', 'nd sev', 'en yea', 'rs ago', '.     ']
Mike Covington
  • 2,147
  • 1
  • 16
  • 26
1

You could use this:

>>> re.findall('(.{6}|.+$)', 'Four score and seven years ago.')
['Four s', 'core a', 'nd sev', 'en yea', 'rs ago', '.']
Szymon
  • 42,577
  • 16
  • 96
  • 114