C# Lua 获取指定字符中间的字符串 正则表达式获取括号里面的字符串

实例代码:获取 [] 里面的内容

 

Lua版本

    print('-----------------')
    for s in string.gmatch('pp[1g1]ppp[1jj2]pp[1413]ppp', '%[(%w+)%]') do
        print(s)
    end
    print('-----------------')

 

 

C#版本

        string str=@"[11][12][1413]";
        foreach(Match m in Regex.Matches(str,@"\[(\w+)\]"))
        {
            print(m.Groups[1].Value);
        }

 

 

下面的表列出了Lua支持的所有字符类:

字符类:(character classes)
. 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

单个字符(除^$()%.[]*+-?外): 与该字符自身配对

.(点): 与任何字符配对
%a: 与任何字母配对
%c: 与任何控制符配对(例如\n)
%d: 与任何数字配对
%l: 与任何小写字母配对
%p: 与任何标点(punctuation)配对
%s: 与空白字符配对
%u: 与任何大写字母配对
%w: 与任何字母/数字配对
%x: 与任何十六进制数配对
%z: 与任何代表0的字符配对
%x(此处x是非字母非数字字符): 与字符x配对. 主要用来处理表达式中有功能的字符(^$()%.[]*+-?)的配对问题, 例如%%与%配对
[数个字符类]: 与任何[]中包含的字符类配对. 例如[%w_]与任何字母/数字, 或下划线符号(_)配对
当上述的字符类用大写书写时, 表示与非此字符类的任何字符配对. 例如, %S表示与任何非空白字符配对.例如,’%A’非字母的字符

‘%’ 用作特殊字符的转义字符,因此 ‘%.’ 匹配点;’%%’ 匹配字符 ‘%’。转义字符 ‘%’不仅可以用来转义特殊字符,还可以用于所有的非字母的字符。当对一个字符有疑问的时候,为安全起见请使用转义字符转义他。

+ 匹配前一字符1次或多次
* 匹配前一字符0次或多次
- 匹配前一字符0次或多次
? 匹配前一字符0次或1次

 

 

【更多参考】

Lua的字符串匹配与正则表达式

https://www.cnblogs.com/meamin9/p/4502461.html

 

正则表达式中文手册

https://github.com/ziishaned/learn-regex/blob/master/translations/README-cn.md

 

在线测试工具

https://regex101.com/

 

相关视频

https://www.bilibili.com/video/BV1da4y1p7iZ?from=search&seid=1887659927205983862

posted @ 2020-12-20 09:59  三页菌  阅读(1205)  评论(0编辑  收藏  举报