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

Question:

Here’s my string:

"5th => fifthn6th => sixthn7th => seventhn8th => eighthn9th => ninthn10th => tenthn11th => eleventhn12th => twelfthn"

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?

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']
Answered By: Joran Beasley

You can use one of these:

>s(.*)

or

>s(w*)

Example:

import re 

s = "5th => fifthn6th => sixthn7th => seventhn8th => eighthn9th => ninthn10th => tenthn11th => eleventhn12th => twelfthn"
# 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 => fifthn6th => sixthn7th => seventhn8th => eighthn9th => ninthn10th => tenthn11th => eleventhn12th => twelfthn"

# 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'}
Answered By: Shahab Rahnama
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.