lua - nginx string.match non posix -


i got string (str1) , want extract after pattern "mycode=",

local str1 = "servername/codebase/?mycode=abc123"; local tmp1 = string.match(str1, "mycode=%w+"); local tmp2 = string.gsub(tmp1,"mycode=", ""); 

from logs,

tmp1 => mycode=abc123 tmp2 => abc123 

is there better/more efficient way this? belive lua strings not follow posix standard (due size of code base).

yes, use capture in pattern control string.match.

from lua reference manual (emphasis mine):

looks first match of pattern in string s. if finds one, match returns captures pattern; otherwise returns nil. if pattern specifies no captures, whole match returned. third, optional numerical argument init specifies start search; default value 1 , can negative.

it works this:

> local str1 = "servername/codebase/?mycode=abc123" > local tmp1 = string.match(str1, "mycode=%w+") > print(tmp1) mycode=abc123 > local tmp2 = string.match(str1, "mycode=(%w+)") > print(tmp2) abc123 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

Maven Javadoc 'Cannot find default setter' and fails -