0

I have a string :

string = 'i-phone is popular - many people like it (user-friendly system, fast, good support)'

How to use regular expression to split it as:

split_string = ['i-phone', 'is', 'popular', 'many', 'people', 'like',  'it', 'user-friendly', 'system', 'fast', 'good', 'support']

The problem is that - contains 2 spaces and 1 hyphen.

I tried: split_string = re.split('[() - ]', string) but I got:

['i-phone', 'is', 'popular', '-', 'many', 'people', 'like', 'it', '', 'user-friendly', 'system,', 'fast,', 'good', 'support', '']

Thanks.

John
  • 691
  • 1
  • 7
  • 20
  • Hope its a duplicate of [https://stackoverflow.com/questions/3939361/remove-specific-characters-from-a-string-in-python/21357173] – yogi Jun 08 '18 at 05:22
  • Thanks, yogi. But I cannot make it. Can you show me? Thanks. – John Jun 08 '18 at 05:29
  • May be some thing like [`\s+(?:[()-]\s*)?|[,()]\s*`](https://regex101.com/r/ltNjjh/1) – Gurmanjot Singh Jun 08 '18 at 05:30
  • @John What do you mean you "cannot make it"? SO is not a "do my work for me" service. It exists to help you fill in *missing knowledge*, and it's up to you to figure out exactly what knowledge it is that you're missing, especially once you have been given a resource to look into. – jpmc26 Jun 08 '18 at 05:39
  • Gurman, thank you. The list obtained is `['i-phone', 'is', 'popular', 'many', 'people', 'like', 'it', 'user-friendly', 'system', 'fast', 'good', 'support', '']` – John Jun 08 '18 at 07:33

1 Answers1

3

Try this regex to split:

\s+(?:[()-]\s*)?|[,()]\s*

Click for Demo

Explanation

  • \s+ - matches 1+ white-spaces
  • (?:[()-]\s*)? - matches either (, ), - followed by 0+ occurrences of white-space. The ? at the end makes this subpart optional
  • | - OR
  • [,()]\s* - matches either , or ( or ) followed by 0+ whitespaces
Gurmanjot Singh
  • 10,224
  • 2
  • 19
  • 43