0

I want a way to create a list out of strings that appear in-between a specific character, in this case ` <-- that

so for example this string:

string = '<#346283347234774> **Useless text that I want to remove**
more useless text it is useless, so useless.
`potato`, `boxes`, `christmas tree`'

becomes this list

list = ['potato', 'boxes', 'christmas tree']

I've tried using regex but I would rather something readable to a total noob that i'll be able to understand when i come back to the code to update it at a later date.

Sorry if this is painfully simple, i'm really new to Python and programming in general.

Ben
  • 49
  • 5
  • This answer might help https://stackoverflow.com/questions/37372603/how-to-remove-specific-substrings-from-a-set-of-strings-in-python – Salma Elshahawy Dec 20 '20 at 12:38

1 Answers1

0

Try using re.findall:

print(re.findall('`([\w+ ]*)`', string))

Output:

['potato', 'boxes', 'christmas tree']

Explanation:

In the above, I have :

`

On both sides of the expression. that just helps to try to match the group since you want the text between those two signs. Whatever is inside the parenthesis gets extracted by the findall function. In the parenthesis we have \w+ which gets the characters inside the two signs, then we have an extra space so that it will also match the space between 'christmas tree', then the * matches everything.

Here is a link for more explanation.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114