I would advise against using regular expressions for this task, because the language you want to parse is not regular.

You have a character string of multiple key value pairs. The best way to parse this is not to match patterns on it, but to properly tokenize it.

There is a module in the Python standard library, called shlex, that mimics the parsing done by POSIX shells, and that provides a lexer implementation that can easily be customized to your needs.

from shlex import shlex

def parse_kv_pairs(text, item_sep=",", value_sep="="):
    """Parse key-value pairs from a shell-like text."""
    # initialize a lexer, in POSIX mode (to properly handle escaping)
    lexer = shlex(text, posix=True)
    # set ',' as whitespace for the lexer
    # (the lexer will use this character to separate words)
    lexer.whitespace = item_sep
    # include '=' as a word character 
    # (this is done so that the lexer returns a list of key-value pairs)
    # (if your option key or value contains any unquoted special character, you will need to add it here)
    lexer.wordchars += value_sep
    # then we separate option keys and values to build the resulting dictionary
    # (maxsplit is required to make sure that '=' in value will not be a problem)
    return dict(word.split(value_sep, maxsplit=1) for word in lexer)

(split has a maxsplit argument, that is much cleaner to use than splitting/slicing/joining.)

Example run:

parse_kv_pairs(
  'key1=value1,key2=\'value2,still_value2,not_key1="not_value1"\''
)

Output:

{'key1': 'value1', 'key2': 'value2,still_value2,not_key1="not_value1"'}

The reason I usually stick with shlex rather than using regular expressions (which are faster in this case) is that it gives you fewer surprises, especially if you need to allow more possible inputs later on. I never found how to properly parse such key-value pairs with regular expressions, there will always be inputs (e.g. A="B=\"1,2,3\"") that will trick the engine.

If you do not care about such inputs, (or, put another way, if you can ensure that your input follows the definition of a regular language), regular expressions are perfectly fine.

Answer from pistache on Stack Overflow
Top answer
1 of 5
14

I would advise against using regular expressions for this task, because the language you want to parse is not regular.

You have a character string of multiple key value pairs. The best way to parse this is not to match patterns on it, but to properly tokenize it.

There is a module in the Python standard library, called shlex, that mimics the parsing done by POSIX shells, and that provides a lexer implementation that can easily be customized to your needs.

from shlex import shlex

def parse_kv_pairs(text, item_sep=",", value_sep="="):
    """Parse key-value pairs from a shell-like text."""
    # initialize a lexer, in POSIX mode (to properly handle escaping)
    lexer = shlex(text, posix=True)
    # set ',' as whitespace for the lexer
    # (the lexer will use this character to separate words)
    lexer.whitespace = item_sep
    # include '=' as a word character 
    # (this is done so that the lexer returns a list of key-value pairs)
    # (if your option key or value contains any unquoted special character, you will need to add it here)
    lexer.wordchars += value_sep
    # then we separate option keys and values to build the resulting dictionary
    # (maxsplit is required to make sure that '=' in value will not be a problem)
    return dict(word.split(value_sep, maxsplit=1) for word in lexer)

(split has a maxsplit argument, that is much cleaner to use than splitting/slicing/joining.)

Example run:

parse_kv_pairs(
  'key1=value1,key2=\'value2,still_value2,not_key1="not_value1"\''
)

Output:

{'key1': 'value1', 'key2': 'value2,still_value2,not_key1="not_value1"'}

The reason I usually stick with shlex rather than using regular expressions (which are faster in this case) is that it gives you fewer surprises, especially if you need to allow more possible inputs later on. I never found how to properly parse such key-value pairs with regular expressions, there will always be inputs (e.g. A="B=\"1,2,3\"") that will trick the engine.

If you do not care about such inputs, (or, put another way, if you can ensure that your input follows the definition of a regular language), regular expressions are perfectly fine.

2 of 5
7

Using some regex magic from Split a string, respect and preserve quotes, we can do:

import re

string = 'key1=value1,key2="value2,still_value2"'

key_value_pairs = re.findall(r'(?:[^\s,"]|"(?:\\.|[^"])*")+', string)

for key_value_pair in key_value_pairs:
    key, value = key_value_pair.split("=")

Per BioGeek, my attempt to guess, I mean interpret the regex Janne Karila used: The pattern breaks strings on commas but respects double quoted sections (potentially with commas) in the process. It has two separate options: runs of characters that don't involve quotes; and double quoted runs of characters where a double quote finishes the run unless it's (backslash) escaped:

(?:              # parenthesis for alternation (|), not memory
[^\s,"]          # any 1 character except white space, comma or quote
|                # or
"(?:\\.|[^"])*"  # a quoted string containing 0 or more characters
                 # other than quotes (unless escaped)
)+               # one or more of the above
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-program-to-extract-key-value-pairs-with-substring-in-a-dictionary
Python program to extract key-value pairs with substring in a dictionary - GeeksforGeeks
April 19, 2023 - In this, we extract all the values with required substring using map() + lambda function. The in operator is used to check for substring inside the key's value. ... # Python3 code to demonstrate working of # Dictionaries with Substring values ...
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-33321.html
How to extract specific key value pair from string?
April 15, 2021 - Hi, I am new to python programming and need your help. I have a Notes Column in excel file which contains data as below ROW 1: Country:USA State:Virginia Serial Number: 00 DB C0 B1 E3 D5 05 7B 57 BE 3A BB FF D1 62 D6 A7 Addr...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ python-program-to-extract-key-value-pairs-with-substring-in-a-dictionary
Python program to extract key-value pairs with substring in a dictionary
July 12, 2023 - Dictionary comprehension provides the most efficient and readable solution for extracting key-value pairs with substrings.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-convert-key-value-pair-comma-separated-string-into-dictionary
Convert key-value pair comma separated string into dictionary โ€“ Python | GeeksforGeeks
January 22, 2025 - This method uses the re module to extract key-value pairs from the string. ... import re # Input string s = "a:1,b:2,c:3" # Extracting key-value pairs using regex matches = re.findall(r'(\w+):(\d+)', s) d = {k: int(v) for k, v in matches} print(d) ... A regular expression matches key-value pairs in the string. re.findall() returns a list of tuples containing the keys and values. A dictionary comprehension converts the tuples into a dictionary, with the values cast to integers. ... Python Tutorial รขย€ย“ Python is one of the most popular programming languages.
Find elsewhere
Top answer
1 of 2
3

You have a few ways to do this. First, since you said your key-value pairs are embedded in a larger email, you need to extract them. You can do that with this regex, which checks for a line starting with a word and an equals sign:

import re

text = " ... Full email text ... "
dataPoints = re.search(r"^\w*=.*$", text, re.MULTILINE).group(0)

Then you need to create your dictionary.

Option 1: Simplest

Use the following regex find:

result = dict(re.findall(r'(\w*)=(\".*?\"|\S*)', dataPoints))

Regex demo

Option 2: Typical split

Follow the typical method for this problem: split the various key-value combinations into a list, and then split each combination into separate keys and values. However, since your key-value pairs are separated by spaces rather than semicolons, ampersands, or something similar, and some of your values have spaces in them, we can't simply split by spaces. That means we need to use a regex lookahead for this to work properly:

regexSplit = dict([i.split("=") for i in re.split(r"\s(?=\w+=)", dataPoints)])

Option 3: No regex

If you want to avoid using regex altogether for whatever reason, you can use the following, which splits on equals signs and then recombines the keys and values into the proper arrangement for creating a dictionary:

allSplits = dataPoints.split("=")
splitList = [allSplits[0]] + [i for a in allSplits[1:-1] 
    for i in a.rsplit(" ", 1)] + [allSplits[-1]]

splitDict = dict(zip(splitList[::2], splitList[1::2]))

The code above assumes your dictionary will end up with at least 2 items.

Demo for all 3 options

2 of 2
0

What about adding an OR (|) to your regex, e.g.

(\w*)=(\"[\w\s\+()\.]*\"|[\w\-\:\.]*)

matches the string you gave.
Note

  • \"[\w\s\+()\.]*\" matches all the values enclosed in ""
  • [\w\-\:\.]* matches the ones without
๐ŸŒ
Alibaba Cloud
alibabacloud.com โ€บ help โ€บ en โ€บ log-service โ€บ latest โ€บ extract-dynamic-key-value-pairs-from-a-string
How to extract dynamic key-value pairs from a string by using different functions - Simple Log Service - Alibaba Cloud Documentation Center
May 19, 2022 - In most cases, use the e_kv function and configure specific parameters to extract key-value pairs, especially when you need to extract and escape enclosed characters or backslashes (\). In complicated or advanced use cases, use the e_regex function ...
Top answer
1 of 2
1

This should do it:

new_dict = dict()
for key, value in orig_dict.items():
    if 'Ethernet' in str(key):
        new_dict[key] = value

When you use print, python calls the __str__ method on the OctetString object, which returns Ethernet8/6. However, I think pprint defaults to printing the object type.

EDIT:

Stefan Pochmann has rightly pointed out below that if 'Ethernet' in will match any string which contains the word Ethernet. The OP did mention using regex in his post to match Ethernet(\d*)/(\d*), so this answer may not be suitable to anyone else looking to solve a similar problem.

2 of 2
1

(I'll use the same 'Ethernet' in str(key) test as the accepted answer.)

If you want to keep the original dict and have the filtered version as a separate dictionary, I'd use a comprehension:

newdict = {key: value
           for key, value in mydict.items()
           if 'Ethernet' in str(key)}

If you don't want to keep the original dict, you can also just remove the entries you don't want:

for key in list(mydict):
    if 'Ethernet' in str(key):
        del mydict[key]

The reason you get "OctetString('...')" is the same as this one:

>>> 'foo'
'foo'
>>> pprint.pprint('foo')
'foo'
>>> print('foo')
foo

The first two tests show you a representation you can use in source code, that's why there are quotes. It's what the repr function gets you. The third test prints the value for normal pleasure, so doesn't add quotes. The "OctetString('...')" is simply such a representation as well, and you can copy&paste it into source code and get actual OctetString objects again, rather than Python string objects. I guess pprint is mostly intended for developing, where it's more useful to get the full repr version.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-extracting-key-from-value-substring
Python โ€“ Extracting Key from Value Substring | GeeksforGeeks
April 11, 2023 - When we work with Python dictionaries, it often involves extracting subsets of key-value pairs based on specific criteria. This can be useful for tasks such as filter ... Given a String, extract all the numbers that are percentages. Input : test_str = 'geeksforgeeks 20% is 100% way to get 200% success' Output : ['20%', '100%', '200%'] Explanation : 20%, 100% and 200% are percentages present.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-extract-key-value-of-dictionary-in-variables
Python | Extract key-value of dictionary in variables - GeeksforGeeks
April 26, 2023 - Method #1: Using items() This problem can be solved using the items function which does the task of extracting a key-value pair and using the 0th index gives us the first key-value pair. Works only in Python2.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-convert-key-value-pair-comma-separated-string-into-dictionary
Convert key-value pair comma separated string into dictionary - Python - GeeksforGeeks
July 11, 2025 - The string is split by commas into a list of key-value pairs. map() applies a lambda function to split each pair by a colon and convert the value to an integer. The dict() function creates a dictionary from the mapped pairs.
๐ŸŒ
Quora
quora.com โ€บ What-is-the-most-efficient-way-to-convert-a-string-of-key-value-pairs-eg-string-a-1-b-2-c-3-into-dictionary-in-Python
What is the most efficient way to convert a string of key=value pairs (eg. string = 'a=1;b=2;c=3;') into dictionary in Python? - Quora
Answer (1 of 2): I would do something like this : [code]the_dict = {} the_string = 'a=1;b=2;c=3;' for pair in the_string.split(';'): if pair: key, value in pair.split('=') the_dict[key.strip()] = value.strip() [/code]you could write this as a comprehension : [code]the_string = '...
๐ŸŒ
GeeksforGeeks
origin.geeksforgeeks.org โ€บ python โ€บ python-extracting-key-from-value-substring
Python - Extracting Key from Value Substring - GeeksforGeeks
April 11, 2023 - Create an empty list result = [] 4. For each key-value pair in the dictionary using items() method a. Check if substring is present in the value of each key-value pair using the in operator b. If the substring is present, append the corresponding ...