string libraries in Lua and powerful pattern-matching learning notes _lua

Source: Internet
Author: User
Tags character classes control characters lua regular expression string format alphanumeric characters

The LUA native interpreter has a very limited ability to handle strings, and powerful string manipulation comes from the string library. The LUA string function is exported in string module. In lua5.1, and also as string-type member methods, we can either write String.upper (s) or s:upper (), and choose the way you prefer.

String.len (s) returns the length of S.
String.rep (s, N) returns a string of n times the repeated s string.
String.Lower (s) returns a copy of the string s that has been converted to lowercase
Lower,upper are all used in the local character set, and if you want to sort a string array, not case-sensitive, you might write like this:

Copy Code code as follows:

Table.sort (A, function (A, B)
Return String.Lower (a) < String.Lower (b)
End

String.sub (S, I, j) extracts a string from S, from I to J (closed range [I, j]), of course you can use negative index values, which will be counted from the tail of the string,-1 is the last character,-2 is the penultimate, and so on, so the advantage is that when we want to extract a few characters to the end, It's convenient to count from the back. For example:
Copy Code code as follows:

s = "[Hello,world]"
Print (String.sub (S, 2,-2))--> Hello,world

Keep in mind that strings in Lua are immutable.

String.char, String.byte is used to convert the values between characters and the numbers for. For example:

Copy Code code as follows:

i = 97
Print (String.char (i, i+1, i+2))--> ABC
Print (String.byte ("abc"))--> 97
Print (String.byte ("abc"),-2)--> 98

lua5.1 String.byte can accept a third parameter, returning multiple values between i,j. For example, this writes a string into an array of character values:

Copy Code code as follows:

t = {s.byte (1,-1}

To convert to a string again:
Copy Code code as follows:

String.char (Unpack (t))

String.Format is a powerful string format function that is similar to the C language printf, and is not described here.

The most powerful functions in the LUA string library are those pattern-matching functions: Find, Match, Gsub, Gmatch. Unlike other scripting languages, Lua has neither a POSIX regular expression nor a regular expression in Perl. The reason for this is that it causes LUA to consume more memory, and Lua's original purpose is to be a compact, embedded language. LUA implements its own set of pattern matching in less than 500 lines of code, although not as strong as a standard regular expression (which typically requires more than 4000 code), but is strong enough.

String.find will find the location where the target template appears in the given string, find the return start and end position, and not find the return nil. For example:

Copy Code code as follows:

s = "Hello,world"
I, j = string.find (S, "Hello")
Print (String.sub (S, I, J))

Of course, String.find can also give you a starting search location, which is useful when you want to find out where all the occurrences are, such as to know where the line breaks appear:
Copy Code code as follows:

Local T = {}
Local i = 0
While True
i = String.find (s, "\ n", i+1)
if i = = Nil then break end
t[#t +1] = i
End

String.match and String.find are similar in that they find the appropriate pattern in the specified string. The difference is that he returns the part of the string that was found:

Copy Code code as follows:

Print (String.match ("Hello,world", "Hello"))--> Hello

For a fixed pattern like "hello", this function has no meaning. But for the variable pattern, it shows his power:
Copy Code code as follows:

Date = "Now is 2014/10/6 17:58"
D = string.match (date, "%d+/%d+/%d+")
Print (d)--> 2014/10/6

The string.gsub has three parameters, a given string, a matching pattern, and an alternate string. The effect is to replace all occurrences of the matching pattern with substitution strings. and returns the replacement string and the number of substitutions.
Copy Code code as follows:

s = String.gsub ("Lua is cute", "cute", "great")
Print (s)--> Lua is great

The String.gmatch function returns an iterator that iterates through all the matching strings that appear in the given string.

Mode:

Character class: (character classes)

Copy Code code as follows:

. All characters
%a Letters
%c control characters
%d digits
%l lower-case Letters
%p Punctuation Characters
%s space characters
%u upper-case Letters
%w alphanumeric characters
%x hexadecimal digits
%z the character whose representation is 0

Their upper-case version is complementary to his own.
Magic characters:
Copy Code code as follows:

( ) . % + - * ? [ ] ^ $

is escaped with%. '%% ' stands for '% '

Character Set (Char-set): Use character sets to customize character classes.

1. Different character classes, and given with []
[%w_] matches alphanumeric characters and underscores.
[01] Matching binary number
2. To include the character range in the character set, between the start and end, plus-
[0-9] equals%d
[0-9a-fa-f] equals%x
3. If you want to be complementary to the character set, precede with ^
[^0-7] Any non-octal number

Repetition or optional modifiers

Copy Code code as follows:

+ 1 or more repetitions, matching the longest,
* 0 or more repetitions the longest
-also 0 or more repetitions shortest
? Optional (0 or 1 occurrence)

Capture

The capture mechanism allows a portion of a pattern string to match part of the target string. Writing is the part of the pattern string that you need to capture to enclose in (), for example:

Copy Code code as follows:

Pair = "name = Anna"
Key, value = String.match (pair, "(%a+)%s*=%s* (%a+)")
Print (key, value)--> name Anna

We can also use the capture for the pattern string itself, "([[\"]) (.-)%1 ", where%1 represents a copy of the first capture.

Replace

As already known, the string.gsub argument can be a string, in fact, it can be a function, or a table, and if it is a function, it will be called with the captured content as an argument and the returned content as a replacement string. If it is a table, the capture is used as the key to fetch the value of the table as a replacement string, and if it does not exist, no substitution is made. Such as:

Copy Code code as follows:

function expand (s)
Return String.gsub (S, "$ (%w+)", _g)
End
Name = "Lua"; Status = "Great"
Print (Expand ("$name is $status, isn ' t it?"))

Finish

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.