What is the alternation operator in Lua patterns?

▼魔方 西西 提交于 2019-12-08 17:45:02

问题


In regex, | is used for alternation. What is the corresponding character in Lua patterns?


回答1:


First, note that Lua patterns are not regular expressions; they are their own simpler matching language (with different benefits and drawbacks).

Per the specification I've linked to above, and per this answer, there is no alternation operator in a Lua pattern. To get this functionality you'll need to use a more powerful Lua construct (like LPEG or a Lua Regex Library).




回答2:


Another workaround is: Instead of:

apple|orange

Write:

[ao][pr][pa][ln][eg]

Explanation: match alternate letters from each word. voila!



来源:https://stackoverflow.com/questions/10438358/what-is-the-alternation-operator-in-lua-patterns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!