Skip to content

Instantly share code, notes, and snippets.

@ChengLuFred
Last active June 25, 2020 00:53
Show Gist options
  • Save ChengLuFred/f22f63e7f447578a634075fe994b7170 to your computer and use it in GitHub Desktop.
Save ChengLuFred/f22f63e7f447578a634075fe994b7170 to your computer and use it in GitHub Desktop.
[Regular Expression] split text according to requirement #Python

Use re(regular expression) to split text

re.split('\W+|_',x)

We use | to seperate symbols that we want to use as spliter. Here \W+ means words. An example is following,

>>>text = 'I:\\Textual Analysis Data\\19950131_10-K_edgar_data_69970_0000950152-95-000069_1.txt'
>>>re.split('\W+',text)
['I',
 'Textual',
 'Analysis',
 'Data',
 '19950131_10',
 'K_edgar_data_69970_0000950152',
 '95',
 '000069_1',
 'txt']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment