开发者

Lua subpattern matching oddity

I have two pieces of code that break up a string based on the patterns.

First one:

local test = "whop^1whap^2whup"
local pattern = "%^[12]"
local lastpos = 1

for startpos, endpos in string.gmatch( test, "()"..pattern.."()" ) do


   print("pos: "..startpos..","..endpos)
   prin开发者_如何学Ct("str: "..string.sub(test,lastpos,startpos-1))
   print("match: "..string.sub(test,startpos,endpos-1))

   lastpos = endpos

end

This one breaks up the string around ^1 or ^2. It outputs:

pos: 5,7
str: whop
match: ^1
pos: 11,13
str: whap
match: ^2

The second version is this:

local test = "whop^t1whap^02whup"
local pattern = "%^[(t1)(02)]"
local lastpos = 1

for startpos, endpos in string.gmatch( test, "()"..pattern.."()" ) do


   print("pos: "..startpos..","..endpos)
   print("str: "..string.sub(test,lastpos,startpos-1))
   print("match: "..string.sub(test,startpos,endpos-1))

   lastpos = endpos

end

This one is SUPPOSED to break up the string at ^t1 or ^02. Instead I get this:

pos: 5,7
str: whop
match: ^t
pos: 12,14
str: 1whap
match: ^0

I noticed that the first pos (5,7) is exactly the same as in the first piece of code, even though the pattern length should be 3 characters.

What am I doing wrong?


Lua patterns are not regular expressions. For example, the pattern [(t1)(02)] does not mean "match the strings 't1' or '02'". It means "match the characters '(', 't', '1', '0', '2', or ')'". It is the lack of features like this in Lua patterns that make them much easier to implement (and therefore makes the Lua standard library smaller).

You have reached the limits of the parse-ability of Lua patterns. If you need regular expressions, I suggest you download a Lua module that implements them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜