аватар question@mail.ru · 01.01.1970 03:00

Using the variable in the regular expression python

Salute! Please help to solve it would seem trivial, but for lack of experience in programming, an absolutely unusual task for me :)

  mt = re.findall ( r '\'" "variable" "\ s \ '' )     

instead of" variable "" should be a variable, which will be consumed by different lines, and I do not know how to present it correctly.

аватар answer@mail.ru · 01.01.1970 03:00

if briefly, then ...

If the variable contains regular expression templates:

  Reg_Exp =  r" "{} \ s'" ". class = "" "> {variable}  \ s'" "     

if the text of the variable contains a literal text:

  Reg_exp =  r" "{} \ s'" ".  format  (Re.escape (Variable)) reg_exp =  fr "''  {re.escape (variable)}  \ s' ""      

regular expressions in Python are asked using string literals, so the problem of using the variable in the regular expression in Python is reduced to the problem of using variables in the string Literal.

Methods of adding a variable to the line

there are many ways:

  • Concathenation of strings (): reg_exp = r "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" r "\ s'" "
  • the formatting operator of the strings %s : reg_exp = r" "'%s \ s'"%Variable
  • : reg_exp = r "{} \ s '" ". Format (variable) or reg_exp = r" "{x} \ s'" "". Format (x = variable)
  • interpolation of strings (available with Python 3.6): reg_Exp = rf "'' {variable} \ s'" "

cm. (Python 3.7.3).

What to choose?

interpolation appeared relatively recently, it is available only in Python starting with version 3.5. It is convenient, since variables are inserted into a string literal as it is in curly brackets. However, it must be remembered that double curly brackets set literal curly brackets, f "{{" " = {, and f"}} " = }

.

method stromat is very similar to interpolation. Having set one argument, it can be repeated as many times by you using {x} , where x & mdash; The serial number of the argument. r "" {0} ': \ s+' {0} '"". Format (variable) announces the expression ' Abc ': \ S+' ABC '. Name arguments simplify (sometimes reduce) the use of variables in the line. r "" {x} ': \ s+' {x} '"". Format (x = variable) as a result will give the same expression as in the previous example. When setting several arguments that are used only once on a line, you can use {} without specifying the index: Another = "" Def "" "'"' {} ': \ s+' {} "" "Format (variable, anoter)) will give Expression 'ABC': \ S+'Def'

. r

Interpolation and curly brackets

Be careful with curly brackets during interpolation: Double curly brackets set literal symbols of { and } , which is very important when using interval quantifiers:

  # Figure brackets    print  ( f ""  {variable}  "" ) r # = & gt; ABC    print  ( f "" {{variable}} "" )  # = & gt; {Variable}    print  ( f "" {{span class = ""> {variable} }} "" )  ######## = & gt; {ABC}      

The same applies to the strFormat method:

   print  (.  format  (variable))  # = & gt; {}   

Special characters in the variable

If the text of the variable can contain special metasims of regular expressions (for example (, ) ,, [, +, *, etc.), it is recommended to use the method re.escape

  Reg_Exp =  r "" {} \ s' "" .  format  (re.escape (Variable))  ^^^^^^^^^^^^^^^^^^^^ul                                      

Latest

Similar