Lua string processing

Source: Internet
Author: User

Match pattern (pattern)

    • . Any single character
    • %a any letter
    • %c any control character
    • %d any number
    • %g any printable character except for whitespace
    • %l All lowercase Letters
    • %p All punctuation
    • %s all whitespace characters
    • %u all uppercase letters
    • %w All letters and numbers
    • %x all 16 binary digit symbols
    • %x (where x is any non-alphabetic/numeric character) represents the character X. such as percent%,%. Represents a point number.%/ Represents a slash/.
    • [set]Represents a union of all the characters in a set, and any one of these characters represents a match. You can use-connect, such as [0-9] to represent 0 to 9 of these 10 numbers.
    • [^set]Represents the set's complement.

Character classes and special meaning symbols:

    • A single character class followed by a ' * ' will match 0 or more of that type of character, matching the longest string possible.
    • A single character class with a '-' will match 0 or more of that type of character, matching the shortest possible string.
    • A single character class followed by a ' + ' will match one or more of the characters of that class, matching the string as long as possible.
    • A single character class with a '? ' will match 0 or one of those characters.
    • %n, where n can be from 1 to 9, matches the captured nth substring.
    • %bxy, where x and Y are two explicit characters, the match begins with X and ends with Y. such as%b () matches a string that includes parentheses.
    • %f[set], Frontier mode. Matches an empty string that precedes a character in the set, and the first character of the position does not belong to set.
string.byte(s[, i[, j]])
    • Returns the numeric encoding of the character S[i], s[i+1], ..., s[j]
    • I default = 1,j default equals i

Cases:

str = "abcdef"print(str:byte()) --> 97,'a' 的编码值print(str:byte(2)) --> 98,'b' 的编码值print(str:byte(2, 4)) -> 98 99  100
string.char(...)
    • Receives an integer as the encoded value of the character as a parameter, returning the corresponding character

Cases:

print(string.char(97, 98, 99) --> abc
string.dump(function [, strip])
    • Represents a function in string form and returns. The returned string can be loaded loadstring.
    • Strip is true, indicating that debug information (local variable name, line number, etc.) is removed from the function

Cases:

local function print_hello()    print("hello world")endlocal f = string.dump(print_hello, true)print(f)local a = loadstring(f)print(type(a)) --> 'function'a() --> 'hello world'
string.find(s, pattern [, init[, plain]])
    • Finds the first match to pattern in the string s, returning the starting and ending positions of the match. No return nil found
    • INIT specifies where to start the lookup, which defaults to 1. A negative value indicates the start of the countdown until the end of the string.
    • Plain is true, the pattern match is turned off.

Cases:

str = "hello, world"print(string.find(str, "llo")) --> 3    5print(string.find(str, "world", -5)) --> 8  12print(string.find(str, "world", -4)) --> nil
string.format(formating, ...)
    • sprintf with C, not supported *, H, L, L, N, p
    • Add%q to format a string as two double quotes.

Cases:

str = 'a string with "quotes" and\nnew line'print(string.format("%q", str))
string.gmatch(s, pattern)
    • Returns an iterator function that, each time it is called, matches s in pattern mode and returns the captured value.

Cases:

s = "hello world from Lua"g = string.gmatch(s, "%a+")print(type(g)) --> 'function', g 是一个函数print(g()) --> 'hello',调用一次则进行一次匹配print(g()) --> 'world',返回第二次匹配的值
s = "from=world, to=Lua"for k, v in string.gmatch(s, "(%w+)=(%w+)") do    print(k, v) --> 打印捕获到的值end
string.gsub(s, pattern, repl[, n])
    • Replaces all matching pattern in a string with REPL and returns the replaced string. The second return value returns the number of matches.
    • No set capture in pattern captures the entire pattern by default
    • Match all By default

Cases:

s = "hello world, hello world, hello world"print(s:gsub("world", "sam")) --> hello sam, hello sam, hello sam   3
    • If there are n parameters, only the first n matches are replaced.

Cases:

s = "hello world, hello world, hello world"print(s:gsub("world", "sam", 1)) --> hello sam, hello world, hello world    1print(s:gsub("world", "sam", 2)) --> hello sam, hello sam, hello world  2
    • If Repl is a string, the string is replaced directly. %d in REPL indicates the number of substrings captured by the first. %0 Represents the entire match. A percent percent represents a single percentile.

Cases:

s = "hello world, hello world, hello world"print(s:gsub("(%w+)", "%1 %1", 1)) --> hello hello world, hello world, hello world  1print(s:gsub("(%w+)%s*(%w+)", "%2 %1", 1)) --> world hello, hello world, hello world    1
    • If REPL is a table, the first captured value is used as the key value for each match to find the table.

Cases:

x = {}x.hello = "HELLO"x.world = "WORLD"s = "hello world, hello world, hello world"print(s:gsub("(%w+)", x)) --> HELLO WORLD, HELLO WORLD, HELLO WORLD 6
    • If Repl is a function, the function is called each time the match is taken, and the capture value is passed in as a parameter to the function.

Cases:

function x(str)    return "sam"ends = "hello world, hello world, hello world"print(s:gsub("(%w+)", x)) --> sam sam, sam sam, sam sam 6
    • The result of a table or function is not manipulated if it is false or nil, and is replaced if it is a string or a number.

Cases:

x = {}x.hello = "HELLO"s = "hello world, hello world, hello world"print(s:gsub("(%w+)", x)) --> HELLO world, HELLO world, HELLO world 6
function x(str)    return nilends = "hello world, hello world, hello world"print(s:gsub("(%w+)", x)) --> hello world, hello world, hello world 6
string.len(s)
    • return string length

Cases:

print(string.len("hello, world")) --> 12
string.lower(s)
    • Uppercase characters in s are converted to lowercase

Cases:

print(string.lower("HEllo, woRLD")) --> hello, world
string.upper(s)
    • Lowercase characters in s are turned into uppercase

Cases:

print(string.upper("HEllo, woRLD")) --> HELLO, WORLD
string.match(s, pattern[, init])
    • Finds the first matching pattern part in a string and returns the captured value. No return nil found.
    • INIT specifies the starting position of the search. The default is 1, which can be negative.

Cases:

s = "hello world, hello world, hello world"print(string.match(s, "hello")) --> helloprint(string.match(s, "wor%a+")) --> world
string.rep(s, n[, sep])
    • Connect n s with Sep and return
    • Default Sep is empty, that is, there is no??? Separator

Cases:

print(string.rep("hello", 2)) --> hellohelloprint(string.rep("hello", 2, ", ")) --> hello, hello
string.reverse(s)
    • Invert string s

Cases:

print(string.reverse("hello")) --> olleh
string.sub(s, i[, j])
    • Returns the substring of S, starting with I, J ends.
    • I and J can be negative.
    • J Default is-1, which is the end of the string.

Cases:

print(string.sub("hello", 2)) --> elloprint(string.sub("hello", 2, 4)) --> ell
string.pack(fmt, v1, v2, ...) string.packsize(fmt) string.unpack(fmt, s[, pos])

Lua string processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.