Regex to detect Python Dictionary Syntax
Question:
I’ve been struggling and my head is about to explode. So I need to write a basic Regex script in Python that will identify if a string has the format (pattern) of a Python dictionary. With the code below, I’ve only managed to successfully match when the string is like this: my_dict = {1: 'apple', 2: 'ball'}
I would like to match something like the below which doesn’t only use numbers as keys and alphabets as values:
{
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
—————Dictionary Detector——————-
# importing regex module
import re
# printing script purpose
# `n` is for printing newline
print('nScript designed to detect whether if user input is a Python dictionary construction.n')
# getting user input
user_inp = input('Type Text> ')
if user_inp != '{}':
user_inp = user_inp.replace('}', ',}', 1)
if re.search('''^{((('(w|d)*')|(w|d)*) *: * (('(w|d)*')|(w|d)*) *, *)*}$''', user_inp):
print('yes, a dictionary has been detected.')
else:
print('No dictionary has been detected.')
Answers:
As mentioned in the comments, json
might be a better fit, but if you still want to do it with regex, here is one:
dict_reg = re.compile(r"""
s* # user might leave spaces in front
{ # the opening curly of dict
( # the key-value pair groups begin
s* # user might leave a space
["'().,w]+ # the "key" part: matches strings, tuples, numbers and variables
s*:s* # the colon and possible spaces around
["'()[].,w]+ # the "value" part: matches strings, tuples, lists, numbers and variables
s* # again, user might leave a space after writing value
,? # the comma that seperates key-value pairs (optional, last one may not have it)
s* # again, user might leave a space
)* # the key-value pair groups as many as possible (* implies empty dict is also ok)
} # the closing curly of dict
s* # again, user might leave a space because why not
""", re.VERBOSE)
You can use as:
re.fullmatch(dict_reg, user_inp)
The apparent cases it does not match:
arithmetic expressions e.g. 2+5
as keys or values
dict
s as values
nested dicts
and possibly many more. But it should do okay for basic dictionaries.
It’s not really a regex solution, but if you are using python, you can do:
def is_dictionary(string) -> bool:
if type(eval(string)) is dict:
return True
else:
return False
I’ve been struggling and my head is about to explode. So I need to write a basic Regex script in Python that will identify if a string has the format (pattern) of a Python dictionary. With the code below, I’ve only managed to successfully match when the string is like this: my_dict = {1: 'apple', 2: 'ball'}
I would like to match something like the below which doesn’t only use numbers as keys and alphabets as values:
{
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
—————Dictionary Detector——————-
# importing regex module
import re
# printing script purpose
# `n` is for printing newline
print('nScript designed to detect whether if user input is a Python dictionary construction.n')
# getting user input
user_inp = input('Type Text> ')
if user_inp != '{}':
user_inp = user_inp.replace('}', ',}', 1)
if re.search('''^{((('(w|d)*')|(w|d)*) *: * (('(w|d)*')|(w|d)*) *, *)*}$''', user_inp):
print('yes, a dictionary has been detected.')
else:
print('No dictionary has been detected.')
As mentioned in the comments, json
might be a better fit, but if you still want to do it with regex, here is one:
dict_reg = re.compile(r"""
s* # user might leave spaces in front
{ # the opening curly of dict
( # the key-value pair groups begin
s* # user might leave a space
["'().,w]+ # the "key" part: matches strings, tuples, numbers and variables
s*:s* # the colon and possible spaces around
["'()[].,w]+ # the "value" part: matches strings, tuples, lists, numbers and variables
s* # again, user might leave a space after writing value
,? # the comma that seperates key-value pairs (optional, last one may not have it)
s* # again, user might leave a space
)* # the key-value pair groups as many as possible (* implies empty dict is also ok)
} # the closing curly of dict
s* # again, user might leave a space because why not
""", re.VERBOSE)
You can use as:
re.fullmatch(dict_reg, user_inp)
The apparent cases it does not match:
arithmetic expressions e.g. 2+5
as keys or values
dict
s as values
nested dicts
and possibly many more. But it should do okay for basic dictionaries.
It’s not really a regex solution, but if you are using python, you can do:
def is_dictionary(string) -> bool:
if type(eval(string)) is dict:
return True
else:
return False