STUDY
STUDY

Reputation: 25

Lua Pattern, problem for combination handle

I want to capture some strings, but how come this is not working? I noticed that using [] it only detects each individual character, I wanted to know if it is possible with more characters

I want to take these combinations, but it's wrong

A ||

Z <<

O ~~~

O..


Current Code:

C = [[

A

B|

C<

Z<<

O~~~

O.

O..

]]

C = C:gsub("(\n%a[(||)(<<)(~~~)(%.%.%.)])",function(a)
print(a) 
end)

Output:

B|

C<

Z<

O~

O.

O.

Upvotes: 1

Views: 142

Answers (1)

Nifim
Nifim

Reputation: 5021

Your Pattern should be something like: (\n%a[|<~%.]+).

Placing a ( inside a lua pattern set just adds ( to the list of chars that could be matched it does not make a "sub-set" or force a required match length.

Lua patterns do not match multiple chars if repeated in a single set. to match multiple chars you need to use the +, * or use multiple instance of the set like this: (\n%a[|<~%.][|<~%.][|<~%.]).

Issues with this are that multiple instances of the set must all match, while if the + is used you have variability in the length of instances you could match such as one . rather than three.

You can not enforce granularity to match 2 different lengths of characters. By this, I mean you can not match specifically O<< and O~~~ in the same pattern while not matching O<<<, O~~ or O<<~.

Resources to learn more about Lua patterns:

FHUG - Understanding Lua Patterns

Upvotes: 0

Related Questions