Lua regex with one match

can anyone help me with lua "regex" aka patterns?

How to convert a regular expression pattern in lua to string.match()

: ytplayer\.config\s*=\s*(\{.+?\});

. You can use this site for an explanation of what this regex does: https://regex101.com/#pcre

Essentially, I want to search for a string that starts with ytplayer.config =

(note the possible spaces before and after the equal sign) and then {

and until we hit the semicolon.

ytplayer.config = {a lot of text, special characters and everything else which is possible...}};

This could be the result.

At the moment I have string.match(s, "ytplayer.config%s=%s({.});")

, but it returns an exact copy (marked with kdiff).

+3


source to share


1 answer


Look, this will return your captured group:

print(string.match("ytplayer.config = {a lot of text, special characters and everything else which is possible...}};", "^ytplayer%.config%s*=%s*({.-});"))

      

Output:

{a lot of text, special characters and everything else which is possible...}}

      



Regular expression ^ytplayer%.config%s*=%s*({.-});

. If you don't want to check the beginning of the line, remove ^

from the beginning.

See this demo .

In Lua templates , %

escspes "magic symbols". Likewise *

, the modifier -

also matches zero or more occurrences of the original class characters. However, instead of matching the longest sequence, it matches the shortest.

+3


source







All Articles