Строковые шаблоны

At the foundation of patterns are character classes. These classes represent sets of characters. Lua's classes are
x (where x is not one of the magic characters ^$()%.[]*+-?) --- represents the character x itself.
. --- represents all characters.
%a --- represents all letters.
%c --- represents all control characters.
%d --- represents all digits.
%l --- represents all lowercase letters.
%p --- represents all punctuation characters.
%s --- represents all space characters.
%u --- represents all uppercase letters.
%w --- represents all alphanumeric characters.
%x --- represents all hexadecimal digits.
%z --- represents the character with representation 0. Note that embedded zeroes in a pattern will not work. Use this instead.

The upper cased version of the above reverses the meaning of said (i.e. %A --- represents all non-letters).
%x (where x is any non-alphanumeric character) --- represents the character x. This is the standard way to escape the magic characters. Any punctuation character (even the non magic) can be preceded by a '%' when used to represent itself in a pattern.
[set] --- represents the class which is the union of all characters in set. A range of characters may be specified by separating the end characters of the range with a '-'. All classes %x described above may also be used as components in set. All other characters in set represent themselves. For example, [%w_] (or [_%w]) represents all alphanumeric characters plus the underscore.
[^set] --- represents the complement of set, where set is interpreted as above.

All patterns are made up of a sequence of these classes. For example, if you wanted to match the string "###Abc" (# designates a digit), you would use the pattern "%d%d%dAbc". By adding modifiers, you can change the meaning of these classes. Let x be the class we want to modify.
x* -- matches 0 or more repetitions of x. Will always match the longest possible chain.
x+ -- matches 1 or more repetitions of x. Will always match the longest possible chain.
x- -- matches 0 or more repetitions of x. Will always match the shortest possible chain.
x? -- matches 0 or 1 occurrence of x.

There are also four special pattern elements
%n -- n must be a number between 1 and 9. Matches the nth captured substring (see below)
%bxy -- matches a substring starting with x and ending with y. The substring must also have the same number of x and y.
^ -- When at the beginning of a pattern, it forces the pattern to match the beginning of a string
$ -- When at the end of a pattern, it forces the pattern to match the end of a string

When ^ or $ is anywhere else in a pattern, it has no special meaning.

Patterns may also have subpatterns. These are enclosed with () and are counted as matches are found. They can be accessed using the %n element mentioned above. Using the empty capture "()" will return the string position where a match is found.

 

Примеры использования:

msg:gsub('"?%w+_?%w*_?%w*"?:',function(w) t_nm[#t_nm+1]=w:gsub('"?:?','') end)

msg:gsub(':"?%w+\+?_?%w*_?%w*"?%.?%d*',function(w) t_val[#t_val+1]=w:gsub('"?:?','') end)

gsub ищет вхождения по шаблону и для каждого найденного применяет анонимную функцию.

Разбор строки JSON:

js_dec:match(':{%p%w+%p+%w+%p+%w+%p+%w+%p}'):gsub('%p%w+%p:',function(w) t_nm[#t_nm+1]=w:gsub('%p','') end)
js_dec:match(':{%p%w+%p+%w+%p+%w+%p+%w+%p}'):gsub(':%p%w+%p',function(w) t_val[#t_val+1]=w:gsub('%p','') end)

Администрирование

Сегодня
Вчера
Эта неделя
Прошлая неделя
Этот месяц
Прошлый месяц
Вся статистика
85
3
85
26686
140
219
26801

IP: 3.139.86.189
Время: 2024-09-16 18:52:24
Счетчик joomla