How to get only a part of a string from a pattern?

Asked

Viewed 129 times

2

I have a string in Lua language. I want to get only the part of filename through the match (403.htm.en).

Example:

var=[[Content-Disposition: attachment; filename="403.htm.en"
     Content-Type: text/plain; name='403.htm.en'
     Content-Transfer-Encoding: BASE64
    ]]
filename = string.match(var,"filename(.+)")
print("filename", filename)

1 answer

2


I don’t know if the most reliable way but this works:

var = [[Content-Disposition: attachment; filename="403.htm.en"
     Content-Type: text/plain; name='403.htm.en'
     Content-Transfer-Encoding: BASE64
    ]]
filename = string.match(var,"filename=\"(.+)\"")
print("filename = ", filename)

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

I had to put the delimiters where you want the text to start and end.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.