Lua Regular Expression (string function), lua Regular Expression

Source: Internet
Author: User
Tags character classes

Lua Regular Expression (string function), lua Regular Expression
The following table lists all the character classes supported by Lua:
. Any character
%
% C control characters
% D Number
% L lowercase letter
% P punctuation
% S blank space character
% U uppercase letters
% W letters and numbers
% X hexadecimal number

% Z represents 0 characters

There are some special characters in pattern matching.

(). % + -*? [^ $

% Is used as the Escape Character for special characters, so '%.' matches character points '.', '%' matches character '% '.

Escape Character '%' can be used not only to escape special characters, but also to all non-letter characters. If you have any questions about a character, use the escape character to escape it for security reasons.

+ Match the previous character once or multiple times
* Match the previous character 0 or multiple times
-Match the previous character 0 or multiple times
? Match the previous character 0 times or 1 time

Example:

(1) Replace non-letters in the string with numbers '.'

Print (string. gsub ("Hello, up-down! "," % ","."))
--> Hello... up. down. 4

(Number 4 is not part of the string result. It is the second result returned by gsub, which indicates the number of replicas. This value will be ignored in other examples about Printing gsub results)


(2) '% d +' matches one or more numbers (integers ):

I, j = string. find ("the number 1298 is even", "% d + ")
--> Print (I, j) --> 12 15


(3) matching Date Format

S = "Today is 30/05/1999, firm"

Print (string. sub (s, string. find (s, "" % d/% d "") --> 30/05/1999


(4) extract the desired file name (the middle part of the file name may be jpg | png | jpeg | gif)

Local str1 = "wkgagkw.m36egcazaahac_mlula790.jpg_200x200_2.jpg"
Local str2 = "wkgagkw.m36egcazaahac_mlula790.png_200x200_2.jpg"


_, Index1 = string. find (str1, "%. % _")
_, Index2 = string. find (str2, "%. % _")
Print (string. sub (str1, 1, index1-1 ))

--> Wkgagkw.m36egcazaahac_mlula790.jpg(match .jpg_or .png _)
Print (string. sub (str2, 1, index2-1 ))

--> Wkgagkw.m36egcazaahac_mlula790.png(matching .jpg_or .png _)
Print (string. gsub (str2, "_ % d + x % d + _ % d %. % a + ",""))

--> Wkgagkw.m36egcazaahac_mlula790.png(matched with _200x200_2.jpg)


Other extensions:

Print ("============================ ".. OS. date ().. "================================= ")

-- View CPU time consumption:

Local x3 = OS. clock ()
Local s = 0
For I = 1, 100000 do
Path, _ = string. gsub (str2, "_ % d + x % d + _ % d %. % a + ","")
End
Local x4 = OS. clock ()
Print (string. format ("Time consumed: %. 2f \ n", x4-x3 ))

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.