How to transform regexp to list

python regex
generate regex from list of strings
regular expression in python for beginners
python regex match word in string
python regex replace all
python regex cheat sheet
regular expression search
regex list

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']]

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

List::Regexp, NAME. regexp_opt - Convert list of strings to a regular expression. SYNOPSIS. use List::Regexp qw(:all);. my $s = regexp_opt(@strings);. Returns a regular expression that will match any string from the input list @strings. First argument can be a reference to a hash, which controls how the regexp is built. First argument can be a reference to a hash, which controls how the regexp is built.

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', '']

String Manipulation and Regular Expressions, To convert the entire string into upper-case or lower-case, you can use the upper​() the regular expression split() method returns a list of all substrings between  The Oracle REGEXP_INSTR function lets you search a string for a regular expression pattern, and returns a number that indicates where the pattern was found. It’s similar to the Oracle INSTR function , but it handles regular expressions where INSTR does not.

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)"

Programmatically Build REGEX (Regular Expression) in Python for , Regular Expression (Regex — often pronounced as ri-je-x or reg-x) is We build a regex for one-line and then we iterate it for all the elements in our list. Here, we'll turn to the go-to Python library for anything regex re . World's simplest string from regexp generator. Just enter your regex in the field below, press Generate Text button, and you get random data that matches your regular expression. Press button, get regex matching strings. No ads, nonsense or garbage.

Regular Expression HOWTO, For example, the regular expression test will match the string test exactly. Here's a complete list of the metacharacters; their meanings will be discussed in If you're not using raw strings, then Python will convert the \b to a backspace, and  ##Regular Expressions. Regular expressions are a tool for matching text patterns in strings of varying length and content. Regexes give you the flexibility to run searches on/match patterns beyond literal fixed characters. The Python module that provides Regex support is called “re”. Search with the re.search() method:

perlrequick, A regex consisting of a word matches any string that contains that word: In list context, a match /regex/ with groupings will return the list of matched values ($1 $x contains "eht tac ni eht tah"; # convert percentage to decimal; $x = "A 39% hit  » Wildcard to regex; Wildcard to regex convert tool. Input your pattern containing wildcard that you want to convert to regex. Regard as wildcard Regex result .

petl.transform.regex, Source code for petl.transform.regex The ``fill`` parameter can be used to provide a list or tuple of values to use if the regular expression does not match. If we want to convert string to list operation, then we need to use the Python string split() method. Python split() method is used to split the strings and store them in the list. The split()  method returns a list of the words in the string, using the “delimiter” as the delimiter string.