首页 > 代码库 > 区别Lua模式匹配中 %a+ 与 .-

区别Lua模式匹配中 %a+ 与 .-

匹配单词与匹配字符

> print(string.gsub("hello!zzy","%a+","tina"))
tina!tina       2
> print(string.gsub("hello!zzy",".-","tina"))
tinahtinaetinaltinaltinaotina!tinaztinaztinaytina       10

%a :匹配字母

.  :匹配任意字符

> print(string.gsub("zzy!jd","%a","~"))~~~!~~  5> print(string.gsub("zzy!jd","%A","~"))zzy~jd  1> print(string.gsub("zzy!jd",".","~"))~~~~~~  6> print(string.gsub("zzy!jd",".-","~"))~z~z~y~!~j~d~   7

 字符短匹配与长匹配:

> test="/*tina*/;/*zz*/"> print(string.gsub(test,"/%*.*%*/","new"))new     1> print(string.gsub(test,"/%*.-%*/","new"))new;new 2

 

区别Lua模式匹配中 %a+ 与 .-