Deleting spaces within words with regex - pre-processing data for text mining

Viewed 79

For a school project I'm working on the kickstarter dataset on Kaggle; https://www.kaggle.com/kemical/kickstarter-projects

In the "name" variable there's a few titles that have spaces in between them; e.g. instance 373 "C R O S S T O W N".

I've been working on some regex all day to re.sub the extra spaces and try to get it to look as a normal word. Though I think this is a problem that occurs more often, most regex content is to add spaces, or add double-spaces. Never this specific task.

I've tried a couple of ways to describe the exact kind of space that needs te be deleted, single out the characters to keep as a group, and using them as the replace string. Though it looks like it should be working, my data doesn't change.

  • long regex expression written to identify words that are written as spaces + single capitals (tried a couple of different ones for this)
  • r'\2\4' refers to the second and fourth group (the first and second alphabetic character)

Names_fixed = []
for i in Name_New: 
    Names_fixed.append(re.sub(r'(\s|^)([A-Z])(\s)(A-Z)\s/g', r'\2\4', i))

As I'm still pretty new to regex, turning to the community for help; thanks a lot in advance.

2 Answers

If your target is only to remove spaces from words, am not sure if you really need regex.

You can use simple replace() function like this:

x = "C R O S S T O W N"
x = x.replace(' ','')

You can run a loop on your list for all such words.

Use this:

re.sub(r'(?<![ \t])[A-Z](?:[ \t][A-Z])+(?![ \t])', lambda x: x.group().replace(' ','').replace('\t',''), i)

Find space/tab-separated words and remove spaces/tabs from the found texts.

EXPLANATION

--------------------------------------------------------------------------------
  (?<!                     look behind to see if there is not:
--------------------------------------------------------------------------------
    [ \t]                    any character of: ' ', '\t' (tab)
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  [A-Z]                    any character of: 'A' to 'Z'
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (1 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    [ \t]                    any character of: ' ', '\t' (tab)
--------------------------------------------------------------------------------
    [A-Z]                    any character of: 'A' to 'Z'
--------------------------------------------------------------------------------
  )+                       end of grouping
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    [ \t]                    any character of: ' ', '\t' (tab)
--------------------------------------------------------------------------------
  )                        end of look-ahead
Related