python - Regex to match only part of certain line -


i have config file need extract values. example, have this:

part {     title = title     description = description here.    // 2 params needed      tags = qwe rty    // don't need param     ... } 

i need extract value of param, example description's value. how do in python3 regex?

here regex, assuming file text in txt:

import re  m = re.search(r'^\s*description\s*=\s*(.*?)(?=(//)|$)', txt, re.m) print(m.group(1)) 

let me explain. ^ matches @ beginning of line. \s* means 0 or more spaces (or tabs) description anchor finding value part. after expect = sign optional spaces before or after denoting \s*=\s*. capture after = , optional spaces, denoting (.*?). expression captured parenthesis. inside parenthesis match (the dot) many times can find (the asterisk) in non greedy manner (the question mark), is, stop following expression matched.

the following expression lookahead expression, starting (?= matches thing right after (?=. , thing 2 options, separated vertical bar |.

the first option, left of bar says // (in parenthesis make atomic unit vertical bar choice operation), is, start of comment, which, suppose, don't want capture. second option $, meaning end of line, reached if there no comment // on line. can after first = sign, until either meet // pattern, or meet end of line. essence of (?=(//)|$) part.

we need re.m flag, tell regex engine want ^ , $ match start , end of lines, respectively. without flag match start , end of entire string, isn't want in case.


Comments