1

I've got a regexp string with only | and () like :

(Hello|Hi) my name is (Bob|Robert)

And I would like to have the complete list of string who match the regexp :

Hello my name is Bob Hello my name is Robert Hi my name is Bob Hi my name is Robert

Is it a tool (librairy) who already do this ?

My first problem is to split the regexp string into a array of array like :

[['Hello','Hi'],'my name is' ,['Bob','Robert']]

3 Answers 3

2

Try exrex, think that should work for you

Simple script

import exrex
print(list(exrex.generate('(Hello|Hi) my name is (Bob|Robert)')))

Output

→ python new_test.py
['Hello my name is Bob', 'Hello my name is Robert', 'Hi my name is Bob', 'Hi my 
name is Robert']

https://github.com/asciimoo/exrex

Sign up to request clarification or add additional context in comments.

Comments

0

Do it with regex:-)

re.split(r"(\(.+?\|.+?\))",s)
Out: ['', '(Hello|Hi)', ' my name is ', '(Bob|Robert)', '']
# and for each string in the list:
re.split(r"\((.+?)\|(.+?)\)",'(Hello|Hi)')
Out: ['', 'Hello', 'Hi', '']

Comments

0

You can try below solution, here I haven't imported any module. The only functions used are strip, split and replace

input_string = "(Hello|Hi) my name is (Bob|Robert)"
split_string = (input_string.replace("("," ").replace(")"," ")).split("  ")
print ([i.strip().split("|") for i in split_string])

#Output --> [['Hello', 'Hi'], ['my name is'], ['Bob', 'Robert']]

I hope this helps!


If you need the final solution to your query then use below code:

from itertools import product
input_string = "(Hello|Hi) my name is (Bob|Robert)"
split_string = (input_string.replace("("," ").replace(")"," ")).split("  ")
jj = [i.strip().split("|") for i in split_string]
kk = list(product(*jj))
print ([" ".join(i) for i in kk])
#output --> ['Hello my name is Bob', 'Hello my name is Robert', 'Hi my name is Bob', 'Hi my name is Robert']

The above code will also work for: input_string = "(Hello|Hi|Hey) my (name|naam) is (Bob|Robert)"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.