Optimal way to set Python regex flags using user defined boolean values [duplicate]










0















This question already has an answer here:



  • Passing in flags as an argument to re.compile

    1 answer



I am building a regex checking tool using python, where the user creates the pattern, checks the flags he/she needs, and then tests different strings using the generated pattern. Python has 6 regex flags: re.I, re.M, re.S, re.U, re.L, and re.X, so writing an if-else statement would be a nightmare, thanks to all the combinations.



Is there a way to define the flags as just boolean values instead? Something like this:



re.compile(pattern, re.IGNORECASE=ignorecase, re.MULTILINE=multiline, ...)



The above code does not work, but hopefully it makes it clear. The pattern, ignorecase, and multiline are all variables defined by the user using a simple form containing an input for the patter, and checkboxes for the flags.



Any help is appreciated.










share|improve this question













marked as duplicate by mkrieger1, Community Nov 12 '18 at 18:31


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • You could easily write a function that converts a list of 6 booleans to any combination of regex flags. However I think I didn't understand what you are trying to do and what exactly the problem is.
    – mkrieger1
    Nov 12 '18 at 18:24










  • Yes, and correct me if I am wrong because math isn't my strong suit, but wouldn't that take a huge number of if-else statements?
    – darkhorse
    Nov 12 '18 at 18:26











  • I think it would take exactly 6 if statements.
    – mkrieger1
    Nov 12 '18 at 18:27











  • If the user is restricted to input the correct alphabet letters for modifiers, then you can just construct a regex with inline modifiers. Example `"(?"+user letters+")(?:"+user regex+")". Then you don't have to worry about passing in flags. It's just a one liner.
    – sln
    Nov 12 '18 at 19:30
















0















This question already has an answer here:



  • Passing in flags as an argument to re.compile

    1 answer



I am building a regex checking tool using python, where the user creates the pattern, checks the flags he/she needs, and then tests different strings using the generated pattern. Python has 6 regex flags: re.I, re.M, re.S, re.U, re.L, and re.X, so writing an if-else statement would be a nightmare, thanks to all the combinations.



Is there a way to define the flags as just boolean values instead? Something like this:



re.compile(pattern, re.IGNORECASE=ignorecase, re.MULTILINE=multiline, ...)



The above code does not work, but hopefully it makes it clear. The pattern, ignorecase, and multiline are all variables defined by the user using a simple form containing an input for the patter, and checkboxes for the flags.



Any help is appreciated.










share|improve this question













marked as duplicate by mkrieger1, Community Nov 12 '18 at 18:31


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • You could easily write a function that converts a list of 6 booleans to any combination of regex flags. However I think I didn't understand what you are trying to do and what exactly the problem is.
    – mkrieger1
    Nov 12 '18 at 18:24










  • Yes, and correct me if I am wrong because math isn't my strong suit, but wouldn't that take a huge number of if-else statements?
    – darkhorse
    Nov 12 '18 at 18:26











  • I think it would take exactly 6 if statements.
    – mkrieger1
    Nov 12 '18 at 18:27











  • If the user is restricted to input the correct alphabet letters for modifiers, then you can just construct a regex with inline modifiers. Example `"(?"+user letters+")(?:"+user regex+")". Then you don't have to worry about passing in flags. It's just a one liner.
    – sln
    Nov 12 '18 at 19:30














0












0








0








This question already has an answer here:



  • Passing in flags as an argument to re.compile

    1 answer



I am building a regex checking tool using python, where the user creates the pattern, checks the flags he/she needs, and then tests different strings using the generated pattern. Python has 6 regex flags: re.I, re.M, re.S, re.U, re.L, and re.X, so writing an if-else statement would be a nightmare, thanks to all the combinations.



Is there a way to define the flags as just boolean values instead? Something like this:



re.compile(pattern, re.IGNORECASE=ignorecase, re.MULTILINE=multiline, ...)



The above code does not work, but hopefully it makes it clear. The pattern, ignorecase, and multiline are all variables defined by the user using a simple form containing an input for the patter, and checkboxes for the flags.



Any help is appreciated.










share|improve this question














This question already has an answer here:



  • Passing in flags as an argument to re.compile

    1 answer



I am building a regex checking tool using python, where the user creates the pattern, checks the flags he/she needs, and then tests different strings using the generated pattern. Python has 6 regex flags: re.I, re.M, re.S, re.U, re.L, and re.X, so writing an if-else statement would be a nightmare, thanks to all the combinations.



Is there a way to define the flags as just boolean values instead? Something like this:



re.compile(pattern, re.IGNORECASE=ignorecase, re.MULTILINE=multiline, ...)



The above code does not work, but hopefully it makes it clear. The pattern, ignorecase, and multiline are all variables defined by the user using a simple form containing an input for the patter, and checkboxes for the flags.



Any help is appreciated.





This question already has an answer here:



  • Passing in flags as an argument to re.compile

    1 answer







python regex






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 18:21









darkhorse

1,42541845




1,42541845




marked as duplicate by mkrieger1, Community Nov 12 '18 at 18:31


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by mkrieger1, Community Nov 12 '18 at 18:31


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • You could easily write a function that converts a list of 6 booleans to any combination of regex flags. However I think I didn't understand what you are trying to do and what exactly the problem is.
    – mkrieger1
    Nov 12 '18 at 18:24










  • Yes, and correct me if I am wrong because math isn't my strong suit, but wouldn't that take a huge number of if-else statements?
    – darkhorse
    Nov 12 '18 at 18:26











  • I think it would take exactly 6 if statements.
    – mkrieger1
    Nov 12 '18 at 18:27











  • If the user is restricted to input the correct alphabet letters for modifiers, then you can just construct a regex with inline modifiers. Example `"(?"+user letters+")(?:"+user regex+")". Then you don't have to worry about passing in flags. It's just a one liner.
    – sln
    Nov 12 '18 at 19:30

















  • You could easily write a function that converts a list of 6 booleans to any combination of regex flags. However I think I didn't understand what you are trying to do and what exactly the problem is.
    – mkrieger1
    Nov 12 '18 at 18:24










  • Yes, and correct me if I am wrong because math isn't my strong suit, but wouldn't that take a huge number of if-else statements?
    – darkhorse
    Nov 12 '18 at 18:26











  • I think it would take exactly 6 if statements.
    – mkrieger1
    Nov 12 '18 at 18:27











  • If the user is restricted to input the correct alphabet letters for modifiers, then you can just construct a regex with inline modifiers. Example `"(?"+user letters+")(?:"+user regex+")". Then you don't have to worry about passing in flags. It's just a one liner.
    – sln
    Nov 12 '18 at 19:30
















You could easily write a function that converts a list of 6 booleans to any combination of regex flags. However I think I didn't understand what you are trying to do and what exactly the problem is.
– mkrieger1
Nov 12 '18 at 18:24




You could easily write a function that converts a list of 6 booleans to any combination of regex flags. However I think I didn't understand what you are trying to do and what exactly the problem is.
– mkrieger1
Nov 12 '18 at 18:24












Yes, and correct me if I am wrong because math isn't my strong suit, but wouldn't that take a huge number of if-else statements?
– darkhorse
Nov 12 '18 at 18:26





Yes, and correct me if I am wrong because math isn't my strong suit, but wouldn't that take a huge number of if-else statements?
– darkhorse
Nov 12 '18 at 18:26













I think it would take exactly 6 if statements.
– mkrieger1
Nov 12 '18 at 18:27





I think it would take exactly 6 if statements.
– mkrieger1
Nov 12 '18 at 18:27













If the user is restricted to input the correct alphabet letters for modifiers, then you can just construct a regex with inline modifiers. Example `"(?"+user letters+")(?:"+user regex+")". Then you don't have to worry about passing in flags. It's just a one liner.
– sln
Nov 12 '18 at 19:30





If the user is restricted to input the correct alphabet letters for modifiers, then you can just construct a regex with inline modifiers. Example `"(?"+user letters+")(?:"+user regex+")". Then you don't have to worry about passing in flags. It's just a one liner.
– sln
Nov 12 '18 at 19:30













1 Answer
1






active

oldest

votes


















0














What you need is a convenient way to combine enabled flags into a single argument that you can pass. Instead of setting a different value for each flag, I would advise simply checking what is set (in whatever way you already do this), and immediately merging the corresponding flag into a single accumulator variable.



For example, supposing the flags were specified as command line arguments I might do it like this:



userchoices = 0
if "-a in opts:
userchoices |= re.A
if "-i" in opts:
userchoices |= re.I
if "-L" in opts:
userchoices |= re.L
etc.

matcher = re.compile(pattern, flags=userchoices)


If you have already written the code that initializes each of your named variables to the corresponding re.? constant, you can simply collect them into an array and OR them together like this:



from operator import or_
from functools import reduce
userchoices = reduce(or_, [asciionly, ignorecase, multiline, ...])


Depending on how you detect the user's selections, you could streamline this further by e.g. using a dictionary to map the options of your form to python constants, etc.



PS. An alternative approach: each flag can be expressed by the corresponding letter and embedded in the regexp itself. So you could do something like this, and bypass the flags argument altogether:



userflags = "".join([asciionly, ignorecase, multiline, ...])
matcher = re.compile("(?%s)%s" % (userflags, pattern) )


I think this is kind of hacky, to be honest, but there may be a place for it since the regexp encapsulates the settings it is being used with.






share|improve this answer






















  • Are you sure this works? I thought the flags would have to be combined using bitwise OR.
    – mkrieger1
    Nov 12 '18 at 18:28










  • Oops, you are right! I was thinking of the flags you embed in a regexp... but still managed to use the flags argument in the snippet :-( ... Hold on, revision coming
    – alexis
    Nov 12 '18 at 18:57










  • Now it matches what the OP wants...
    – alexis
    Nov 12 '18 at 19:21

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














What you need is a convenient way to combine enabled flags into a single argument that you can pass. Instead of setting a different value for each flag, I would advise simply checking what is set (in whatever way you already do this), and immediately merging the corresponding flag into a single accumulator variable.



For example, supposing the flags were specified as command line arguments I might do it like this:



userchoices = 0
if "-a in opts:
userchoices |= re.A
if "-i" in opts:
userchoices |= re.I
if "-L" in opts:
userchoices |= re.L
etc.

matcher = re.compile(pattern, flags=userchoices)


If you have already written the code that initializes each of your named variables to the corresponding re.? constant, you can simply collect them into an array and OR them together like this:



from operator import or_
from functools import reduce
userchoices = reduce(or_, [asciionly, ignorecase, multiline, ...])


Depending on how you detect the user's selections, you could streamline this further by e.g. using a dictionary to map the options of your form to python constants, etc.



PS. An alternative approach: each flag can be expressed by the corresponding letter and embedded in the regexp itself. So you could do something like this, and bypass the flags argument altogether:



userflags = "".join([asciionly, ignorecase, multiline, ...])
matcher = re.compile("(?%s)%s" % (userflags, pattern) )


I think this is kind of hacky, to be honest, but there may be a place for it since the regexp encapsulates the settings it is being used with.






share|improve this answer






















  • Are you sure this works? I thought the flags would have to be combined using bitwise OR.
    – mkrieger1
    Nov 12 '18 at 18:28










  • Oops, you are right! I was thinking of the flags you embed in a regexp... but still managed to use the flags argument in the snippet :-( ... Hold on, revision coming
    – alexis
    Nov 12 '18 at 18:57










  • Now it matches what the OP wants...
    – alexis
    Nov 12 '18 at 19:21















0














What you need is a convenient way to combine enabled flags into a single argument that you can pass. Instead of setting a different value for each flag, I would advise simply checking what is set (in whatever way you already do this), and immediately merging the corresponding flag into a single accumulator variable.



For example, supposing the flags were specified as command line arguments I might do it like this:



userchoices = 0
if "-a in opts:
userchoices |= re.A
if "-i" in opts:
userchoices |= re.I
if "-L" in opts:
userchoices |= re.L
etc.

matcher = re.compile(pattern, flags=userchoices)


If you have already written the code that initializes each of your named variables to the corresponding re.? constant, you can simply collect them into an array and OR them together like this:



from operator import or_
from functools import reduce
userchoices = reduce(or_, [asciionly, ignorecase, multiline, ...])


Depending on how you detect the user's selections, you could streamline this further by e.g. using a dictionary to map the options of your form to python constants, etc.



PS. An alternative approach: each flag can be expressed by the corresponding letter and embedded in the regexp itself. So you could do something like this, and bypass the flags argument altogether:



userflags = "".join([asciionly, ignorecase, multiline, ...])
matcher = re.compile("(?%s)%s" % (userflags, pattern) )


I think this is kind of hacky, to be honest, but there may be a place for it since the regexp encapsulates the settings it is being used with.






share|improve this answer






















  • Are you sure this works? I thought the flags would have to be combined using bitwise OR.
    – mkrieger1
    Nov 12 '18 at 18:28










  • Oops, you are right! I was thinking of the flags you embed in a regexp... but still managed to use the flags argument in the snippet :-( ... Hold on, revision coming
    – alexis
    Nov 12 '18 at 18:57










  • Now it matches what the OP wants...
    – alexis
    Nov 12 '18 at 19:21













0












0








0






What you need is a convenient way to combine enabled flags into a single argument that you can pass. Instead of setting a different value for each flag, I would advise simply checking what is set (in whatever way you already do this), and immediately merging the corresponding flag into a single accumulator variable.



For example, supposing the flags were specified as command line arguments I might do it like this:



userchoices = 0
if "-a in opts:
userchoices |= re.A
if "-i" in opts:
userchoices |= re.I
if "-L" in opts:
userchoices |= re.L
etc.

matcher = re.compile(pattern, flags=userchoices)


If you have already written the code that initializes each of your named variables to the corresponding re.? constant, you can simply collect them into an array and OR them together like this:



from operator import or_
from functools import reduce
userchoices = reduce(or_, [asciionly, ignorecase, multiline, ...])


Depending on how you detect the user's selections, you could streamline this further by e.g. using a dictionary to map the options of your form to python constants, etc.



PS. An alternative approach: each flag can be expressed by the corresponding letter and embedded in the regexp itself. So you could do something like this, and bypass the flags argument altogether:



userflags = "".join([asciionly, ignorecase, multiline, ...])
matcher = re.compile("(?%s)%s" % (userflags, pattern) )


I think this is kind of hacky, to be honest, but there may be a place for it since the regexp encapsulates the settings it is being used with.






share|improve this answer














What you need is a convenient way to combine enabled flags into a single argument that you can pass. Instead of setting a different value for each flag, I would advise simply checking what is set (in whatever way you already do this), and immediately merging the corresponding flag into a single accumulator variable.



For example, supposing the flags were specified as command line arguments I might do it like this:



userchoices = 0
if "-a in opts:
userchoices |= re.A
if "-i" in opts:
userchoices |= re.I
if "-L" in opts:
userchoices |= re.L
etc.

matcher = re.compile(pattern, flags=userchoices)


If you have already written the code that initializes each of your named variables to the corresponding re.? constant, you can simply collect them into an array and OR them together like this:



from operator import or_
from functools import reduce
userchoices = reduce(or_, [asciionly, ignorecase, multiline, ...])


Depending on how you detect the user's selections, you could streamline this further by e.g. using a dictionary to map the options of your form to python constants, etc.



PS. An alternative approach: each flag can be expressed by the corresponding letter and embedded in the regexp itself. So you could do something like this, and bypass the flags argument altogether:



userflags = "".join([asciionly, ignorecase, multiline, ...])
matcher = re.compile("(?%s)%s" % (userflags, pattern) )


I think this is kind of hacky, to be honest, but there may be a place for it since the regexp encapsulates the settings it is being used with.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 '18 at 19:20

























answered Nov 12 '18 at 18:27









alexis

33.5k954114




33.5k954114











  • Are you sure this works? I thought the flags would have to be combined using bitwise OR.
    – mkrieger1
    Nov 12 '18 at 18:28










  • Oops, you are right! I was thinking of the flags you embed in a regexp... but still managed to use the flags argument in the snippet :-( ... Hold on, revision coming
    – alexis
    Nov 12 '18 at 18:57










  • Now it matches what the OP wants...
    – alexis
    Nov 12 '18 at 19:21
















  • Are you sure this works? I thought the flags would have to be combined using bitwise OR.
    – mkrieger1
    Nov 12 '18 at 18:28










  • Oops, you are right! I was thinking of the flags you embed in a regexp... but still managed to use the flags argument in the snippet :-( ... Hold on, revision coming
    – alexis
    Nov 12 '18 at 18:57










  • Now it matches what the OP wants...
    – alexis
    Nov 12 '18 at 19:21















Are you sure this works? I thought the flags would have to be combined using bitwise OR.
– mkrieger1
Nov 12 '18 at 18:28




Are you sure this works? I thought the flags would have to be combined using bitwise OR.
– mkrieger1
Nov 12 '18 at 18:28












Oops, you are right! I was thinking of the flags you embed in a regexp... but still managed to use the flags argument in the snippet :-( ... Hold on, revision coming
– alexis
Nov 12 '18 at 18:57




Oops, you are right! I was thinking of the flags you embed in a regexp... but still managed to use the flags argument in the snippet :-( ... Hold on, revision coming
– alexis
Nov 12 '18 at 18:57












Now it matches what the OP wants...
– alexis
Nov 12 '18 at 19:21




Now it matches what the OP wants...
– alexis
Nov 12 '18 at 19:21



這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3