code: def AdvSearch(document, search, bs=3):
      '''Return set of all regex matches
      This is an advanced version of python-docx.search() that takes into
      account blocks of <bs> elements at a time.
      What it does:
      It searches the entire document body for text blocks.
      Since the text to search could be spawned across multiple text blocks,
      we need to adopt some sort of algorithm to handle this situation.
      The smaller matching group of blocks (up to bs) is then adopted.
      If the matching group has more than one block, blocks other than first
      are cleared and all the replacement text is put on first block.
      Examples:
      original text blocks : [ 'Hel', 'lo,', ' world!' ]
      search : 'Hello,'
      output blocks : [ 'Hello,' ]
      original text blocks : [ 'Hel', 'lo', ' __', 'name', '__!' ]
      search : '(__[a-z]+__)'
      output blocks : [ '__name__' ]
      @param instance  document: The original document
      @param str       search: The text to search for (regexp)
                            append, or a list of etree elements
      @param int       bs: See above
      @return set      All occurences of search string
      '''
      searchre = re.compile(search)
      matches = []
      searchels = []
      for element in document.iter():
          if element.tag == '{%s}t' % nsprefixes['w']:
              if element.text:
                  searchels.append(element)
                  if len(searchels) > bs:
                      searchels.pop(0)
                  found = False
                  for l in range(1, len(searchels)+1):
                      if found:
                          break
                      for s in range(len(searchels)):
                          if found:
                              break
                          if s+l <= len(searchels):
                              e = range(s, s+l)
                              txtsearch = ''
                              for k in e:
                                  txtsearch += searchels[k].text
                              match = searchre.search(txtsearch)
                              if match:
                                  matches.append(match.group())
                                  found = True
      return set(matches)