-2

I think what I want to do is a fairly common task but I've found no reference on the web.

I have a list of names, with the following pattern

'first name middle name last name last seen 10 months ago"

I want to keep only the names, to delete all the string starting from the word last, is there a way I can do it?

Example:

output = ' David Smith last seen 8 months ago'

desired_output = 'David Smith'

I thought using regular expression, but I didn't succeed.

Thanks

Babr
  • 83
  • 8

2 Answers2

1

You could use split() to get the left side of the "last" keyword:

string = ' David Smith last seen 8 months ago'
name   = string.split("last",1)[0].strip() # 'David Smith'
Alain T.
  • 40,517
  • 4
  • 31
  • 51
0

I found a way to do it with re, thank you all.

output = ' David Smith last seen 8 months ago'

desired_output = re.sub(r'\slast+\s\w+\s\w\s\w+\s\w+', ' ', output)
Babr
  • 83
  • 8