Regex from `=>` until 1st `\n`

Viewed 49

Here's my string:

"5th => fifth\n6th => sixth\n7th => seventh\n8th => eighth\n9th => ninth\n10th => tenth\n11th => eleventh\n12th => twelfth\n"

It was read from a .txt file, and the output above is what it prints. The "\n" represents newline. In the text file, I would have:

5th => fifth
6th => sixth

I've tried substitution_regex = re.compile(r"(?==>(.*?)\\)"), which according to this link should work, but when I do substitution_regex.search(string), I get None.

Why is that?

2 Answers

\\ is attempting to match a literal \ character

\n is simply not matched always unless you specify re.DOTALL

simply exclude the \\ from your regex

you will also need to use a greedy regex

>>> substitution_regex = re.compile(r"=>(.*)")   
>>> substitution_regex.findall(s)                
[' fifth', ' sixth', ' seventh', ' eighth', ' ninth', ' tenth', ' eleventh', ' twelfth']

You can use one of these:

>\s(.*)

or

>\s(\w*)

Example:

import re 

s = "5th => fifth\n6th => sixth\n7th => seventh\n8th => eighth\n9th => ninth\n10th => tenth\n11th => eleventh\n12th => twelfth\n"
# substitution_regex = re.compile(r">\s(.*)")
substitution_regex = re.compile(r">\s(\w*)")
substitution_regex.findall(s) 

Output:

['fifth',
 'sixth',
 'seventh',
 'eighth',
 'ninth',
 'tenth',
 'eleventh',
 'twelfth']

For your purpose:

Use this for finding keys:

(\w*)\s=

Code:

import re 

s = "5th => fifth\n6th => sixth\n7th => seventh\n8th => eighth\n9th => ninth\n10th => tenth\n11th => eleventh\n12th => twelfth\n"

# Extract keys in list : ['5th', '6th', ...]
keys_regex = re.compile(r"(\w*)\s=")
keys = keys_regex.findall(s)  

# Extract Values in list : ['fifth', 'sixth', ...]
values_regex = re.compile(r">\s(\w*)")
values = values_regex.findall(s) 
 
# Making a dictionary from keys and values
result = dict(zip(keys, values))

print(result)

Output:

{'5th': 'fifth', '6th': 'sixth', '7th': 'seventh', '8th': 'eighth', '9th': 'ninth', '10th': 'tenth', '11th': 'eleventh', '12th': 'twelfth'}
Related