code not behaving properly but I can't find the problem

Hello everyone, I have this problem that is driving me crazy.
This is the idea behind the code:
I download a site, open the downloaded file, I make some modifications on the html code, save it and open the modified html with webview.
I added an event listener for when you click a link inside the webview to start the linkDownload function that does the same thing I explained above so every time you click a link inside the webview I modify the html and show what I want it to show.
When you first open the webview everything loads correctly. Once I click a link though, it is failing to find that first p1 from the htmlToText_Corsi (text) function and thus leaving p1 at nil and then executing the if part that has p1 == nil.
The print(text) just below it shows the html code, and the string i’m trying to find is there! so why is p1 getting a nil value from that find??
I’ve tried modifying the string to find a much simpler one just for testing and still have the same issue.

local function webListener( event )  
  
 local function linkDownload ( event )  
 if ( event.isError ) then  
 print ( "Network error - download failed" )  
 else  
 local htmlFile = io.open( path, "r" )  
 local html = htmlFile:read( "\*a" ) --codice html del sito scaricato  
 io.close( htmlFile )  
 htmlExtracted = html2text.htmlToText({ caller = "menuCorsi", text = html})  
 htmlCode = text2html.textToHtml({ caller = "menuCorsi", text = htmlExtracted})   
 --This code writes "htmlCode" to an HTML file and saves it in the temporary directory.  
 local htmlFile = io.open( path, "w" )  
 htmlFile:write( htmlCode )  
 io.close( htmlFile )  
 local x, y = display.screenOriginX, display.screenOriginY + 20.5\*2  
 local width, height = \_realW, \_realH - y  
 webView:request( "uniRomaTreTemporary.html", system.DocumentsDirectory )  
 end  
 end  
  
 if event.url then  
 print( "You are visiting: " .. event.url )  
 end  
  
 if event.type then  
 print( "The event.type is " .. event.type ) -- print the type of request  
 end  
  
 if event.errorCode then  
 native.showAlert( "Errore nell'aprire il link", event.errorMessage, { "OK" } )  
 end  
  
 if event.type == "link" then  
 webView:stop()  
 print("------------STOPPING WEBVIEW----------------")  
 network.download( event.url, "GET", linkDownload, "uniRomaTreTemporary.html", system.DocumentsDirectory )  
 end  
  
end  
  
local function urlDownload ( event )   
 if ( event.isError ) then  
 print ( "Network error - download failed" )  
 else  
 local htmlFile = io.open( path, "r" )  
 local html = htmlFile:read( "\*a" ) --codice html del sito scaricato  
 io.close( htmlFile )  
 htmlExtracted = html2text.htmlToText({ caller = "menuCorsi", text = html})  
 htmlCode = text2html.textToHtml({ caller = "menuCorsi", text = htmlExtracted})   
 --This code writes "htmlCode" to an HTML file and saves it in the temporary directory.  
 local htmlFile = io.open( path, "w" )  
 htmlFile:write( htmlCode )  
 io.close( htmlFile )  
 local x, y = display.screenOriginX, display.screenOriginY + 20.5\*2  
 local width, height = \_realW, \_realH - y  
 webView = native.newWebView( x, y, width, height )  
 webView:request( "uniRomaTreTemporary.html", system.DocumentsDirectory )  
 webView:addEventListener( "urlRequest", webListener )  
 end  
end  
  
 network.download( gridP.urls.corsi, "GET", urlDownload, "uniRomaTreTemporary.html", system.DocumentsDirectory )  

Here is the code of the funcion that is called above in the line: htmlExtracted = html2text.htmlToText({ caller = “menuCorsi”, text = html}) in both linkDownload and urlDownload.

function htmlToText\_Corsi (text)  
  
 local mainURL = "https://portalestudente-s3.uniroma3.it/esse3/"  
 local p1, p2, p3, p4, posStart, posEnd = nil  
 text = string.gsub (text, '\n', "")  
 p1, p2 = string.find(text, '

| **Percorsi di studio/Curricula** |

')  
 print(text)  
 if ( p1 ~= nil ) then --secondo livello di navigazione?  
 print("---------VERIFICANDO SECONDO LIVELLO---------------")  
 p3, p4 = string.find(text, '', p2)  
 posStart = p1  
 posEnd = p4  
 text = (string.sub(text, posStart, posEnd))  
 end  
  
 if( p1 == nil ) then --primo livello di navigazione?  
 print("---------VERIFICANDO PRIMO LIVELLO---------------")  
 p1, p2 = string.find(text, '

')  
 posStart = p1  
 p3, p4 = string.find(text, ' |', posStart)  
 posEnd = p3-1  
 text = (string.sub(text, posStart, posEnd))  
 end  
  
 text = string.gsub (text, 'width%="[^%"]\*%" ', "")  
 text = string.gsub (text, 'href%="', 'href="'..mainURL)  
  
 return text  
  
end  

I’m testing the app on an Android device (samsung galaxy tablet, android 2.2)

Thanks for your attention [import]uid: 165560 topic_id: 32813 reply_id: 332813[/import]

Nevermind, I found the problem,
apparently

[code]p1, p2 = string.find(text, ’

| Percorsi di studio/Curricula |

')[/code]

wasn’t finding anything because of bad written pattern although I find it weird because i’ve used similar stuff in other parts and they work. [import]uid: 165560 topic_id: 32813 reply_id: 130553[/import]

Nevermind, I found the problem,
apparently

[code]p1, p2 = string.find(text, ’

| Percorsi di studio/Curricula |

')[/code]

wasn’t finding anything because of bad written pattern although I find it weird because i’ve used similar stuff in other parts and they work. [import]uid: 165560 topic_id: 32813 reply_id: 130553[/import]