匹配带特殊字符的字符串

str = [[<?php
code
code
?>]]

print(string.match(str, "<?"))    //实际输出: < , 预期输出: <?
print(string.match(str, "<?php")) //实际输出: php , 预期输出: <?php
print(string.find(str, "<?"))     //实际输出: 1 1 , 预期输出: 1 2
print(string.match(str, "<?php")) //实际输出: 3 5 , 预期输出: 1 5

请解释这个神秘的输出原因并提供一个解决方案以生成所需的输出。

点赞
用户5387592
用户5387592

Lua match 使用模式,而不仅仅是一个精确的字符串。

更多解释请参考手册

请查看模式和魔法字符。

2017-03-19 11:11:37