title: Select part of text that was extracted using the Tesseract OCR
body: <p>I'm using the latest Tesseract OCR engine in R to extract text from a couple of images. It works pretty well and I'm happy with the results. The problem is that I don't want the whole text, just some part, but I don't know how to extract it.</p>
  
  <p>Code is this: </p>
  
  <pre><code>library("tesseract") 
  library("pdftools")
  library("magick")
  
  mypdfFile&lt;-"C:/Users/.../fileName.pdf"
  
  mypngFile&lt;-pdf_convert(mypdfFile, format="png", pages=1, dpi=600)
  
  myImage&lt;-image_read("fileName_1.png")
  
  textFile&lt;-ocr(myImage,engine = tesseract("spa"), HOCR = FALSE) # Text is in spanish
  
  cat(textFile) 
  </code></pre>
  
  <p>Now, the end result looks like this</p>
  
  <pre><code>bla bla bla bla bla bla 
  bla text that I want to 
  extract bla bla bla bla 
  bla bla bla bla bla bla  
  </code></pre>
  
  <p>How can I get the <strong>text that I want to extract</strong> and only that? </p>
  
  <p>I tried to crop the image before applying the ocr() function, but it's not feasible or very accurate to just crop that part. ocr() returns plain text.  </p>
  
  <p><em>Full example below</em></p>
  
  <p>The image (originally a pdf file) is an electricity bill. I can't provide it in full due to privacy issues, but it looks like this <a href="https://i.stack.imgur.com/ACfU9.jpg" rel="nofollow noreferrer">sample image</a>. Under <strong>NOMBRE Y DIRECCION</strong> (name and address), there should be two lines (one with the name and the other with the address) followed by "GALEANA CENTRO LERDO. C.P. " (the name of the city) and "35150 LERDO,DGO." (zip code and state). My code looks like this </p>
  
  <pre><code>myImage&lt;-image_read("sampleImage.png")
  
  myImage&lt;-image_crop(myImage, new dimensions) #crop the right half and some from the top
  
  textFile&lt;-ocr(myImage,engine = tesseract("spa"), HOCR = FALSE) 
  
  cat(textFile) 
  </code></pre>
  
  <p>I get </p>
  
  <pre><code>Nombre y Domicilio
  NAME REDACTED 
  ADDRESS REDACTED
  GALEANA CENTRO LERDO. C.P.
  35150 LERDO, DGO.
  Cuenta E Tarifa
  30DC27B011164660 General &lt; 25kW 02
  AE A MA E
  Num. de Lectura Lectura Mult. C
  Medidor actual anterior
  BD6687 40994 40539 1 ¿
  Apoyo gubernamental
  </code></pre>
  
  <p>I just want to extract from this everything between "NAME REDACTED" and "35150 LERDO, DGO." inclusive.  </p>