2

I am writing a tool which accepts user configuration via a json file. One piece of this config is a python regular expression and some optional regex flags. Currently the configuration for the regex flags is an array of integers which will all be run through a bitwise or (|) and sent to the re compile method.

My question is how can I validate these integers to ensure that they are valid re flags?

EDIT: Or potentially another solution to my problem... Is it possible for the user to specify the actual re flags in the JSON? I.e., [re.DEBUG, re.IGNORECASE] etc etc and then somehow translate those from the JSON file in my python script?

6
  • Is it for Python 2 or 3? Commented Sep 10, 2017 at 21:19
  • @WiktorStribiżew Ideally I would like both to be supported Commented Sep 10, 2017 at 21:22
  • 1
    It seems you may use if option in [2, 4, 8, 16, 32, 64, 128, 256] to validate these. Just note that re.ASCII (256) is only present in Python 3 re. Commented Sep 10, 2017 at 21:28
  • @WiktorStribiżew what about my edit? Do you think its possible to allow the user to specify the name of the flag instead? That would be a lot more descriptive and helpful Commented Sep 10, 2017 at 21:32
  • 1
    Then you may create a dictionary and map the names with the real flags, i.e. option_dct = { 're.I' : re.I, etc. } Commented Sep 10, 2017 at 21:38

1 Answer 1

1

You can define a dictionary of all possible flags (they are really few, see re 6.2.2. Module Contents), and just get the value by the corresponding key.

A Python demo:

import re
re_flags = { 're.A' : re.A, 
    're.ASCII' : re.ASCII,
    're.DEBUG' : re.DEBUG,
    're.I' : re.I,
    're.IGNORECASE' : re.IGNORECASE,
    're.L' : re.L,
    're.LOCALE' : re.LOCALE,
    're.M' : re.M,
    're.MULTILINE' : re.MULTILINE,
    're.S' : re.S,
    're.DOTALL' : re.DOTALL,
    're.X' : re.X,
    're.VERBOSE' : re.VERBOSE }
flg = 're.I'                      # User input
if flg in re_flags:               # If the dict contains the key
    print(re_flags[flg])          # Print the value (re.I = 2)

If you still want to go with numbers instead:

import re
print(re.A)           # 256
print(re.ASCII)       # 256
print(re.DEBUG)       # 128
print(re.I)           # 2
print(re.IGNORECASE)  # 2
print(re.L)           # 4
print(re.LOCALE)      # 4
print(re.M)           # 8
print(re.MULTILINE)   # 8
print(re.S)           # 16
print(re.DOTALL)      # 16
print(re.X)           # 64
print(re.VERBOSE)     # 64
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah I assigned them to the raw numbers because i don't know whether my script will be being run in python 2 or 3 so if I try to use the re module there are some flags which do not exist in python 2 etc

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.