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

lua 字符串 正则表达式 转义 特殊字符的更多相关文章

  1. python正则表达式转义注意事项

    无论哪种语言,在使用正则表达式的时候都避免不了一个问题,就是在匹配元字符的时候,需要对元字符进行转义,让 正则表达式引擎将其当做普通字符来匹配.本文主要以python为例,说明一下转义中需要注意的问题 ...

  2. Lua 学习之基础篇三<Lua 字符串操作>

    Lua字符串可以使用以下三种方式表示: 单引号间的一串字符. 双引号间的一串字符. [[和]]间的一串字符. string = [["Lua"]] print("字符串 ...

  3. 第11.14节 正则表达式转义符和Python转义符相同引发问题的解决办法

    正则表达式使用反斜杠('\')来把特殊字符转义成普通字符(为了方便称为"正则表达式转义"),而反斜杠在普通的 Python 字符串里也是转义符(称为"字符串转义" ...

  4. Lua字符串库(整理)

    Lua字符串库小集 1. 基础字符串函数:    字符串库中有一些函数非常简单,如:    1). string.len(s) 返回字符串s的长度:    2). string.rep(s,n) 返回 ...

  5. 1.C#基础学习笔记3---C#字符串(转义符和内存存储无关)

    技术qq交流群:JavaDream:251572072  教程下载,在线交流:创梦IT社区:www.credream.com ------------------------------------- ...

  6. atitit.js的 字符串内容 转义  js处理html

    atitit.js的 字符串内容 转义  js处理html 1. js处理html的问题 1 2. js的 字符串内容 转义 1 2.1. 处理流程 1 3. 下面的表格列出了其余的特殊字符,这些特殊 ...

  7. 第11.15节 Python正则表达式转义符定义的特殊序列

    一. 引言 在前面<第11.13节 Python正则表达式的转义符"\"功能介绍>介绍了正则表达式转义符'\',只不过当时作为转义符主要是用于在正则表达式中表示元字符自 ...

  8. java 正则匹配空格字符串 正则表达式截取字符串

    java 正则匹配空格字符串 正则表达式截取字符串 需求:从一堆sql中取出某些特定字符串: 比如配置的sql语句为:"company_code = @cc and project_id = ...

  9. C#格式化字符串中转义大括号“{}”

    原文:C#格式化字符串中转义大括号"{}" 今天,用C#写程序操作Excel,读取单元格内容根据所需格式生成字符串,使用String.Format(string format,ob ...

随机推荐

  1. HTTP Status 500 - The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application 错误

    解决方法链接:http://stackoverflow.com/questions/17709076/http-status-500-the-absolute-uri-http-java-sun-co ...

  2. MVC 文件上传问题

    在用MVC作文件上传处理时,最开始是这样的. html代码 <div id="dialog" title="Upload files">       ...

  3. 不得不说的wepapi 优化

    1:在你的ASP.NET Web API中使用GZIP 或 Deflate .对于减少响应包的大小和响应速度,压缩是一种简单而有效的方式.这是一个非常有必要使用的功能,你可以查看更多关于压缩的文章在我 ...

  4. 【webGL】threejs入门 ---创建一个简单立方体

    开发环境 Three.js是一个JavaScript库,所以,你可以使用平时开发JavaScript应用的环境开发Three.js应用.如果你没什么偏好的话,我会推荐Komodo IDE. 调试建议使 ...

  5. Linux kilin 安装和按键服务器步骤

    U盘启动,傻瓜式安装 设置root 密码 sudo passwd root vsftpd和ssh服务 使用apt-get可以直接安装vsftpd和ssh,首先要sudo apt-get update ...

  6. 模拟搭建Web项目的真实运行环境(七)

    下面这个是mongo驱动的小案例,里面也有涉及到一点redis的操作 https://github.com/SuperRocky/MyMongoDriver 接下来通过几张图片主要介绍一下每个文件的具 ...

  7. 动态创建DAL层类的实例

    为了可扩展性,方便以后对于代码的修改维护,使用动态创建DAL层对象. 1.首先在webconfig中的configuration下添加配置项 <appSettings> <add k ...

  8. Golang 语法学习笔记

    Golang 语法学习笔记 包.变量和函数. 包 每个 Go 程序都是由包组成的. 程序运行的入口是包 main. 包名与导入路径的最后一个目录一致."math/rand" 包由 ...

  9. 用javascript设置和读取cookie的例子

    请看下面用javascript设置和读取cookie的简单例子,现在的问题是,如果要设置的是一个cookie集,比如在cookie1集中有uname,uid两组信息,应该如何写呢?cookie(&qu ...

  10. AOJ 0558 Cheese【BFS】

    在H * W的地图上有N个奶酪工厂,分别生产硬度为1-N的奶酪.有一只吃货老鼠准备从老鼠洞出发吃遍每一个工厂的奶酪.老鼠有一个体力值,初始时为1,每吃一个工厂的奶酪体力值增加1(每个工厂只能吃一次), ...