You need to find in the text some text that is between two characters (for example, "@")

templateTest = 'ddddffs@rusjjd@sdsvvv@xcvdfb @' simvol = '@' def findReplasement(sourse, start, end): regular = re.compile('%a(.*?)%b' %(start,end), re.IGNORECASE) result = regular.findall(sourse) return result x = findReplasement(templateTest, simvol,simvol) print(x) 

But in this case, an error is issued:

ValueError: unsupported format character 'b' (0x62) at index 8

At the same time, if you do not use variables, then everything works:

 regular = re.compile('@(.*?)@', re.IGNORECASE) 

['rusjjd', 'xcvdfb']

Where is the mistake?

  • and stupidly start + '(.*?)' + end ? - splash58
  • very strange, but it was the first version, and when specifying the arguments to the "start" function, it recognized as the sum of add and gave an error. and now earned, thanks! - medyaniklis
  • operator % conversion type b - Sour Sourse

3 answers 3

The problem is that in the expression

 '%a(.*?)%b' %(start,end) 

%b means converting an integer expression (to a binary system), and you are in a function call

 findReplasement(templateTest, simvol,simvol) 

use the simvol parameter of type string .

It is necessary to use (twice - also for %a ) the format descriptor %s for the string type - that means, instead of

 regular = re.compile('%a(.*?)%b' %(start,end), re.IGNORECASE) 

use

 regular = re.compile('%s(.*?)%s' %(start,end), re.IGNORECASE) 

and the output from your program will be

['rusjjd', 'xcvdfb ']

    Try this:

     In [88]: pat = re.compile(r'{}(.*?){}'.format(simvol,simvol), flags=re.I) In [89]: re.findall(pat, templateTest) Out[89]: ['rusjjd', 'xcvdfb '] 

      To find all the text fragments between the specified start, end boundaries using your regular expression:

       import re def findall_inbetween(text, start, end): return re.findall('(?i)' + '(.*?)'.join(map(re.escape, [start, end])), text) 

      re.escape() used to shield characters that are special in regular expressions. For example . (dot) is replaced by \. (to literally find a point, not an arbitrary character).

      Example:

       >>> findall_inbetween('a@b@c@d@', *'@'*2) ['b', 'd']