美文网首页
Groovy字符串与正则

Groovy字符串与正则

作者: zhaoyubetter | 来源:发表于2017-04-19 21:54 被阅读1114次

字符串

  • 单引号,类似于Java中的String,不会对$进行处理,即就是字符串;
    def h = 'Hello $ better' // 输出的 Hello $ better
  • 双引号,如果字符串中有$符号,会对$表达式进行先求值,没有则跟单引号类似;
    def i = 128;
    def str = 'Hello $i' // 输出的是 Hello 128
  • 三引号,‘’‘XXX’‘’,就字符串进行换行输出;

字符串操作

可通过索引来获取字符串中的子串;

// 索引
def better = 'better'
better[2]    // t
better[-1]   // r index from end
println(better[0..2])   // bet
println(better[4..2])   // ett

// 方法,Groovy新增一些String的一些方法,如:center、reverse等等
def message = "better"
println(message.center(15))                 // better 居中
println(message.center(15, "*"))   // ****better*****
println(message.padLeft(15, "*"))  // *********better
println(message.tokenize('e'))      // [b, tt, r]

1..2 被称为范围(range)

groovy 中正则的定义:

在Groovy中 groovy 正则使用 ~'regex' 定义正则,也可以使用 // 来定义,如:

// ==== groovy 正则创建 ====
def regex = ~'better'       // 创建正则,将字符串编译成 Pattern
def match = 'zhaoyubetter' =~ 'better'  // 将操作符左边的字符串跟右边的Pattern进行局部匹配,返回为Mather
def match1 = 'zhaoyubetter' =~ regex

println(match.find())   // true
println(match1.find())   // true

// === 精确匹配 ==~(左边整个字符串与右边的模式) ==
// 类似 matcher.matches()
println( 'zhaoyubetter' ==~ 'better')   // false

// ===== 支持 // 来定义正则 (意思是不要写2个\\)=====
regex = ~/\w+@\w+(\.\w+)+/
println(('zhaoyubetter@126.com' =~ regex).find())   // true

分组匹配

// ==== 分组匹配 ====
// Groovy 简化了matcher操作,可把Matcher对象看作一个二维矩阵。
// 第1维表示每一个与模式相匹配的字符串;
// 第2维表示每个匹配内的捕获组(capture group)

// android 中 为 xml 指定id的方式如下:
def str1 = "@+id/button1"
def str3 = "@id/button3"
def str2 = "@android:id/button2"

println("----- 分组获取 -----")
pattern = ~/@((\+id)|(id)|(android:id))\/(.*)/
matcher = str1 =~ pattern
println("分类1:${matcher[0][2]} ${matcher[0][3]} ${matcher[0][4]} ${matcher[0][5]}")
matcher = str2 =~ pattern
println("分类2:${matcher[0][2]} ${matcher[0][3]} ${matcher[0][4]} ${matcher[0][5]}")
matcher = str3 =~ pattern
println("分类3:${matcher[0][2]} ${matcher[0][3]} ${matcher[0][4]} ${matcher[0][5]}")

输出:

----- 分组获取 -----
分类1:+id null null button1
分类2:null null android:id button2
分类3:null id null button3

统计代码正则分析:

java 程序中,注释分为2种,一种 // ,一种 /**/(存在多行的情况),我们先来定义正则如下:

    def regex1 = ~/^\s*\/\/.*/          // 表示注释1
    def regex2 = ~/^\s*\/\*.*\*\/\s*$/  // 表示注释2
    def regex2_start = ~/^\s*\/\*.*/    // 表示注释2头
    def regex2_end = ~/.*\*\/\s*$/      // 表示注释2尾
    def regex_space = ~/^\s*\s*$/       // 空行
   
    // 可以使用下面的代码进行测试
    def explainStr1 = 'def a = \'ddd\' //cc'
    def explainStr2 = '/***wojfjsjf'
    def explainStr3 = '*/  '
    def explainStr4 = '/*adfdfsfdsdf*/'
    def spaceLineStr = '    '

测试没问题后,进行程序类的编写如下:

class CodeLineCountRegex2 {
    // ===== 注释类型 ===
// 我是注释1  单号
// ---> /* 我是注释2  头
// ---> */ 我是注释2  尾

    def regex1 = ~/^\s*\/\/.*/          // 表示注释1
    def regex2 = ~/^\s*\/\*.*\*\/\s*$/  // 表示注释2
    def regex2_start = ~/^\s*\/\*.*/    // 表示注释2头
    def regex2_end = ~/.*\*\/\s*$/      // 表示注释2尾
    def regex_space = ~/^\s*\s*$/       // 空行

    def explainLine = 0
    def codeLine = 0
    def spaceLine = 0
    def regex_begin = false       // 是否注释2开始了
   
   // 文件的递归遍历
    def codeLineCount(File file) {
        if (file.exists()) {
            if (file.isDirectory()) {
                file.eachFile { current ->
                    codeLineCount(current)
                }
            } else {
                anylise(file)
            }
        }
    }
   
   // 分别分析每一个文件
    def anylise(file) {
        file.each { line ->
            if (regex_begin) {   // 是否注释2开始了
                explainLine++
                if ((line =~ regex2_end).matches()) {
                    explainLine++
                    regex_begin = false
                }
            } else {
                if ((line =~ regex1).matches()) {
                    explainLine++
                } else if ((line =~ regex2).matches()) {
                    explainLine++
                } else if ((line =~ regex2_start).matches()) {
                    regex_begin = true
                } else if ((line =~ regex_space).matches()) {
                    spaceLine++
                } else {
                    codeLine++
                }
            }
        }
    }
}

测试程序如下:

def file = new File("/Users/zhaoyu/Documents/Java/Pattern/src/com/better/groovy/base/CharTimes.groovy")

if(file.exists()) {
    println("文件存在")

    CodeLineCountRegex2 regex2 = new CodeLineCountRegex2()
    regex2.codeLineCount(file)


    println("行数:${regex2.codeLine}")
    println("注释:${regex2.explainLine}")
    println("空行:${regex2.spaceLine}")
}

相关文章

  • Groovy字符串与正则

    字符串 单引号,类似于Java中的String,不会对$进行处理,即就是字符串;def h = 'Hello $ ...

  • Groovy基础

    字符串 groovy与Java字符串的区别是,groovy的字符串拥有运算能力,单引号和双引号都可以定义一个字符串...

  • python3代码片段

    正则匹配检查 正则提取字符串 获取日期时间字符串 时间暂停 取得当前py文件的路径 字符串替换(注意与js的rep...

  • Gradle开发-Groovy字符串

    ##Groovy 字符串 前言:因为笔者本身就有 Java 基础,因此在学习 Groovy 时,主要学习 Groo...

  • web前端入门到实战:JS字符串首字母都大写的几种方法

    1、For循环 2、For循环+replace 1与2写法差别不大 3、正则+replace 思路:用正则将字符串...

  • 正则表达式学习笔记

    基本 正则表达式是使用你定义的“规则”扫描目标字符串,寻找目标字符串中与“规则”相匹配的字符串。在线练习正则表达式...

  • JavaScript正则表达式

    正则表达式 描述字符串规则的表达式 regexOBJ.test(str) 测试正则表达式与指定字符串是否匹配,但是...

  • 【程序员笔试】+Java+字符串

    字符串比较与创建对象 字符数组与字符串互转 subString 正则表达式---- 1 字符串这种特殊的内存机制是...

  • 正则基础

    语法:/正则表达式主体/修饰符(可选) search()方法 用于检索字符串中指定的子字符串,或检索与正则表达式相...

  • Jenkins实践指南-03-Groovy基础知识02

    2.4.4 数据类型 2.4.4.1 字符串类型 2.4.4.1.1 字符串定义     在Groovy中字符中是...

网友评论

      本文标题:Groovy字符串与正则

      本文链接:https://www.haomeiwen.com/subject/auahzttx.html