How to transform regexp to list
up vote
1
down vote
favorite
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']]
python string split
add a comment |
up vote
1
down vote
favorite
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']]
python string split
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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']]
python string split
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']]
python string split
python string split
asked Nov 9 at 7:29
axel584
1646
1646
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
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
add a comment |
up vote
0
down vote
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', '']
add a comment |
up vote
0
down vote
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)"
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
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
add a comment |
up vote
2
down vote
accepted
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
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
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
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
answered Nov 9 at 7:46
Richy
30318
30318
add a comment |
add a comment |
up vote
0
down vote
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', '']
add a comment |
up vote
0
down vote
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', '']
add a comment |
up vote
0
down vote
up vote
0
down vote
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', '']
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', '']
answered Nov 9 at 7:46
kantal
60717
60717
add a comment |
add a comment |
up vote
0
down vote
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)"
add a comment |
up vote
0
down vote
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)"
add a comment |
up vote
0
down vote
up vote
0
down vote
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)"
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)"
edited Nov 9 at 11:46
answered Nov 9 at 9:35
Akash Swain
813
813
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53221452%2fhow-to-transform-regexp-to-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown