lua中字符串匹配笔记

string.gsub 函数有三个参数:目标串,模式串,替换串。
基本作用是用来查找匹配模式的串,并将使用替换串其替换掉: 

s = string.gsub("Lua is good", "good", "bad?") 
print(s)   --> Lua is bad

string.gsub 的第二个返回值表示进行替换操作的次数。例如,
下面代码计字符串中空格出现的次数: 

_, count = string.gsub("test test", " ", " ") 
_ 表示哑元变量

模式串
.   任意字符 
%a   字母 
%c   控制字符 
%d   数字 
%l   小写字母 
%p   标点字符 
%s   空白符 
%u   大写字母 
%w   字母和数字 
%x   十六进制数字 
%z   代表 0的字符 


特殊字符如下:
(). % + - * ? [ ^ $ 
% 也作为以上特殊字符的转义字符。

[] 该方框作为匹配该范围的集合,。
  如[0-9] 则匹配0到9的数字范围

Lua 中的模式修饰符有四个: 
+   匹配前一字符 1 次或多次,最长匹配
*   匹配前一字符 0 次或多次,最长匹配
-   匹配前一字符 0 次或多次,最短匹配
?   匹配前一字符 0 次或 1次 
'+',匹配一个或多个字符,总是进行最长的匹配。
如,模式  '%a+'  匹配一个或多个字母或者一个单词: 
注意以上的区别:


如:匹配c中的注释串
用 '/%*.*%*/'  和'/%*.-%*/'
str = "int x; /* x */  int y; /* y */" 
print(string.gsub(str, "/%*.*%*/", "<注释串>")) 
  --> int x; <注释串> 
采用 '.-' 则为最短匹配,即匹配 "/*" 开始到第一个 "*/"  之前的部分: 
str = "int x; /* x */  int y; /* y */" 
print(string.gsub(str, "/%*.-%*/", "<注释部分>")) 
  --> int x; <注释串>  int y; <注释串> 

以 '^'  开头表示只匹配目标串的开始部分,
以 '$'  结尾表示只匹配目标串的结尾部分。

%b 表示匹配对称字符,注意其只能针对的ansi码单字符。
x = string.gsub("xdddddyxxx", "%bxy", "取代")
print(x)   -->取代xxx

如去除字符串首尾的空格: 
function trim (s) 
  return (string.gsub(s, "^%s*(.-)%s*$", "%1")) 
end 


---------------------------------
看原文中的gsub注释:相当详细,不过对于模式解释补充在上。

string.gsub (s, pattern, repl [, n])

Returns a copy of s in which all occurrences of the pattern 
have been replaced by a replacement string specified by repl,
which may be a string, a table, or a function. 
gsub also returns, as its second value, the total number of substitutions made.


repl是字符串,则为替换。 如果在参数前有%则表示符合匹配的字符串
If repl is a string, then its value is used for replacement. 
The character % works as an escape character:
any sequence in repl of the form %n, with n between 1 and 9, stands for the 
value of the n-th captured substring (see below). 
The sequence %0 stands for the whole match. The sequence %% stands for a single %.


repl作为表参数
If repl is a table, then the table is queried for every match, 
using the first capture as the key; if the pattern specifies 
no captures, then the whole match is used as the key.


如果参数为函数,则每次匹配成功则调用该函数
If repl is a function, then this function is called every 
time a match occurs, with all captured substrings passed 
as arguments, in order; 

if the pattern specifies no captures,
then the whole match is passed as a sole argument.

If the value returned by the table query or by the function call is a string or a number, 
then it is used as the replacement string; otherwise, if it is false or nil, 
then there is no replacement (that is, the original match is kept in the string).

参数n则限制最大
The optional last parameter n limits the maximum number of substitutions to occur.


举例:
   %1 表示符合模式的第一个匹配
   x = string.gsub("hello world", "(%w+)", "%1 %1")
   --> x="hello hello world world"
     
   第4项
   x = string.gsub("hello world", "%w+", "%0 %0", 1)
   --> x="hello hello world"
   
   hello 和from作为模式中左匹配为%1,world 和lua为右匹配,为%2
   x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1")
   --> x="world hello Lua from"


   替换 以$打头的字符串
   x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv)
   --> x="home = /home/roberto, user = roberto"
   
   参数为函数类型
   x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s)
           return loadstring(s)()
         end)
     --> x="4+5 = 9"
     
    参数为表类型
   local t = {name="lua", version="5.1"}
   x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t)
   --> x="lua-5.1.tar.gz"

==============================
gmatch 的用法:
在模式符合匹配多次时
Returns an iterator function that, each time it is called,
returns the next captures from pattern over string s. 
If pattern specifies no captures, then the whole match 
is produced in each call.

看例子:
   s = "hello world from Lua"
   for w in string.gmatch(s, "%a+") do
      print(w)
   end
  
采用gmatch来解析到表类型
     t = {}
     s = "from=world, to=Lua"
     for k, v in string.gmatch(s, "(%w+)=(%w+)") do
       t[k] = v
     end

一个http传送参数的应用
URL 编码,
这种编码将一些特殊字符(比  '=' '&' '+')转换为"%XX"形式的编码,
XX是字符的16进制表示,空白为'+'。
如,将"a+b = c"  编码为 "a%2Bb+%3D+c" 
一个典型的参数串:
name=al&query=a%2Bb+%3D+c&q=yes+or+no 


1先把空格和16进制编码转换为ansi码
function convet1(s) 
  s = string.gsub(s, "+", " ") 
  s = string.gsub(s, "%%(%x%x)", function (h) 
   return string.char(tonumber(h, 16)) 
  end) 
  return s 
end 

2.解析参数串
p = {} 
function decode (s) 
  for name, value in string.gmatch(s, "([^&=]+)=([^&=]+)") do 
  name = unescape(name) 
  value = unescape(value) 
  p[name] = value 
  end 
end 


本文转自:http://www.cnitblog.com/kenlistian/archive/2008/10/15/50292.html

<think>我们正在讨论Lua字符串匹配和截取操作。根据用户要求,需要参考站内引用,但更应关注用户上一次的问题和当前需求。用户当前需求:查找关于Lua字符串匹配和截取的方法或示例。根据引用[1]中提到的string.gsub函数,用于字符串替换,但用户需要的是匹配和截取。引用[2]提到了string.sub(s,i,j)用于截取字符串。引用[3]提到string.find(),string.match(),string.gmatch()等函数用于匹配,需要pattern参数。引用[4]提到了字符串格式化中的截取(如%5.3s),但这不是我们要讨论的重点。因此,我们重点介绍:1.字符串匹配string.find,string.match,string.gmatch2.字符串截取:string.sub此外,用户可能需要一些示例。注意:回答中需要包含引用标识,如引用[2]提到string.sub,则应在相应位置标注[^2]。由于用户要求方法或示例,我们将分别给出这些函数的说明和示例。开始组织答案:首先,介绍字符串匹配的函数:-string.find:查找模式在字符串中的位置-string.match:返回匹配模式的子串-string.gmatch:返回一个迭代器,用于遍历字符串中所有匹配模式的子串其次,介绍字符串截取:-string.sub:根据索引截取字符串然后,给出一些示例。注意:用户要求用中文回答,并且行内公式使用$...$,但这里没有数学公式,所以我们可以忽略数学格式的要求。我们将自然地引用参考内容:例如:引用[2]提到string.sub(s,i,j)用于返回剪切的字符串[^2]。但请注意,引用[1][3][4]也可能有相关的内容,我们根据相关性进行引用。根据引用[3]:这些函数(string.find(),string.match(),string.gmatch()和string.gsub())通常需要一个pattern作为参数[^3]。所以我们在介绍这些匹配函数时,要提醒注意pattern的构建。下面开始写回答。</think> 在 Lua 中,字符串匹配和截取操作主要通过以下函数实现,结合引用内容提供详细方法和示例: ### 一、字符串匹配方法 1. **`string.find()`** 查找模式首次出现的位置: ```lua local s = "Lua is powerful" local start, end_pos = string.find(s, "power") -- 返回 8, 12 print(string.sub(s, start, end_pos)) --> "power" ``` *引用:需谨慎构建 pattern 避免返回 nil [^3]* 2. **`string.match()`** 提取首个匹配模式的子串: ```lua local date = "2023-10-05" local year = string.match(date, "%d%d%d%d") -- 返回 "2023" ``` 3. **`string.gmatch()`** 迭代提取所有匹配项(返回迭代器): ```lua local s = "apple,banana,orange" for fruit in string.gmatch(s, "([^,]+)") do print(fruit) --> 依次输出 apple, banana, orange end ``` ### 二、字符串截取方法 **`string.sub()`** 通过索引截取子串(索引从 1 开始): ```lua local s = "Lua Programming" print(string.sub(s, 5, 15)) --> "Programmin" print(string.sub(s, -5, -1)) --> "ming"(倒数索引) ``` *引用:格式为 `string.sub(s,i,j)`,i/j 为起止索引 [^2]* ### 三、进阶组合用法 1. **截取特定模式内容**(匹配+截取结合): ```lua local email = "[email protected]" local name = string.sub(email, 1, string.find(email, "@")-1) --> "user" ``` 2. **格式化截取**(引用格式化语法): ```lua print(string.format("%.3s", "banana")) --> "ban"(截取前3字符) ``` *引用:`%n.s` 格式可截取前 n 个字符 [^4]* 3. **模式替换时截取片段**: ```lua local s = string.gsub("Color: red; Size: large", "(%a+):%s(%a+)", "【%1=%2】") print(s) --> "【Color=red】; 【Size=large】" ``` *引用:`gsub` 的替换串可用 %n 引用匹配项 [^1]*
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值