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

Time:09-06

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?

CodePudding user response:

\\ 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']

CodePudding user response:

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']
  • Related