Given a list of strings. The task is to split the string by the given list of strings.
Input : test_str = ‘geekforgeeksbestforgeeks’, sub_list = [“best”]
Output : [‘geekforgeeks’, ‘best’, ‘forgeeks’]
Explanation : “best” is extracted as different list element.
Input : test_str = ‘geekforgeeksbestforgeeksCS’, sub_list = [“best”, “CS”]
Output : [‘geekforgeeks’, ‘best’, ‘forgeeks’, “CS”]
Explanation : “best” and “CS” are extracted as different list element.
Method : Using re.split() + | operator
In this, we perform the task of split using regex split() with | operator to check for all the words that need to be put separately.
The original string is : geekforgeeksisbestforgeeks The segmented String : ['geekforgeeksis', 'best', 'forgeeks']
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.