-2

I have a list as following:

list12 = ['**FIRS0425 SOPL ZTE First Company limited', 'Apple Technology','*ROS Sami']

My code is as following

import re
[item2 for item in list12 for item2 in item.split() if not re.match("^[*A-Z]+(0-9){4}$", item2)]

I got output like :

['First', 'Company', 'limited', 'Apple', 'Technology', 'Sami']

I expect the output to be like :

['SOPL', 'ZTE', 'First', 'Company', 'limited', 'Apple', 'Technology', 'ROS', 'Sami']

I am not good with regular expression. How can I reach to my required solution?

Vas
  • 598
  • 1
  • 3
  • 12
  • Why not `**FIRS0425` ? There's a `*` as well. – Jan Feb 19 '19 at 15:21
  • You may use `if not re.match(r"\**[A-Z]+[0-9]{4}$", item2)`. Note it will output `'*ROS'`, not `'ROS'`. See [demo](https://ideone.com/K4hYaN). However, the same output can be achieved if you replace `(0-9)` with `[0-9]` in your pattern. – Wiktor Stribiżew Feb 19 '19 at 15:24

2 Answers2

0

Seems you're looking for

\b([A-Za-z]+)\b

In Python:

import re
list12 = ['**FIRS0425 SOPL ZTE First Company limited', 'Apple Technology','*ROS Sami']

rx = re.compile(r'\b([A-Za-z]+)\b')
result = [word for item in list12 for word in rx.findall(item)]
print(result)

Which yields

['SOPL', 'ZTE', 'First', 'Company', 'limited', 'Apple', 'Technology', 'ROS', 'Sami']
Jan
  • 38,539
  • 8
  • 41
  • 69
  • This solution is similar but i have a huge text and the code which you wrote is having 2 for loops and consuming most of my time. – Vas Feb 19 '19 at 15:49
0

A non-regex way in python,

list12 = ['**FIRS0425 SOPL ZTE First Company limited', 'Apple Technology','*ROS Sami']
str = " ".join(list12)
list21 = str.split()
res = [k.strip('*') for k in list21 if '**' not in k]
print(res)

Output:

['SOPL', 'ZTE', 'First', 'Company', 'limited', 'Apple', 'Technology', 'ROS', 'Sami']

DEMO: http://tpcg.io/s9aBhe

Always Sunny
  • 29,081
  • 6
  • 43
  • 74