Jason
Jason

Reputation: 143

What is the alternation operator in Lua patterns?

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

Upvotes: 11

Views: 5558

Answers (3)

Lua does not have alternations in patterns you cannot use (test1|test2). You can only choose between multiple characters like [abcd] will match a, b, c or d.

Upvotes: 3

Alex
Alex

Reputation: 15

Another workaround is: Instead of:

apple|orange

Write:

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

Explanation: match alternate letters from each word. voila!

Upvotes: -5

Phrogz
Phrogz

Reputation: 303208

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).

Upvotes: 20

Related Questions