从groovy中的字符串中提取数字数据

时间:2013-03-22 14:13:35

标签: string groovy

我得到一个可以包含文本和数字数据的字符串:

示例:

“100磅” “我认为173磅” “73磅。”

我正在寻找一种简洁的方法来仅从这些字符串中提取数字数据。

以下是我正在做的删除回复的内容:

def stripResponse(String response) {
    if(response) {
        def toRemove = ["lbs.", "lbs", "pounds.", "pounds", " "]
        def toMod = response
        for(remove in toRemove) {
            toMod = toMod?.replaceAll(remove, "")
        }
        return toMod
    }
}

6 个答案:

答案 0 :(得分:24)

您可以使用findAll然后将结果转换为整数:

def extractInts( String input ) {
  input.findAll( /\d+/ )*.toInteger()
}

assert extractInts( "100 pounds is 23"  ) == [ 100, 23 ]
assert extractInts( "I think 173 lbs"   ) == [ 173 ]
assert extractInts( "73 lbs."           ) == [ 73 ]
assert extractInts( "No numbers here"   ) == []
assert extractInts( "23.5 only ints"    ) == [ 23, 5 ]
assert extractInts( "positive only -13" ) == [ 13 ]

如果您需要小数和负数,则可以使用更复杂的正则表达式:

def extractInts( String input ) {
  input.findAll( /-?\d+\.\d*|-?\d*\.\d+|-?\d+/ )*.toDouble()
}

assert extractInts( "100 pounds is 23"   ) == [ 100, 23 ]
assert extractInts( "I think 173 lbs"    ) == [ 173 ]
assert extractInts( "73 lbs."            ) == [ 73 ]
assert extractInts( "No numbers here"    ) == []
assert extractInts( "23.5 handles float" ) == [ 23.5 ]
assert extractInts( "and negatives -13"  ) == [ -13 ]

答案 1 :(得分:1)

通过metaClass添加以下方法numbersFilter后,您可以按以下方式调用它:

assert " i am a positive number 14".numbersFilter() == [ 14 ]
assert " we 12 are 20.3propaged 10.7".numbersFilter() == [ 12,20.3,10.7 ]
assert " we 12 a20.3p 10.7 ,but you can select one".numbersFilter(0) == 12
assert " we 12 a 20.3 pr 10.7 ,select one by index".numbersFilter(1) == 20.3

将此代码添加为BootStrap

String.metaClass.numbersFilter={index=-1->
            def tmp=[];
            tmp=delegate.findAll( /-?\d+\.\d*|-?\d*\.\d+|-?\d+/ )*.toDouble()
            if(index<=-1){
                return tmp;
            }else{
                if(tmp.size()>index){
                    return tmp[index];
                }else{
                   return tmp.last();
                }
            }

}

答案 2 :(得分:1)

Putting this here for people that also need this.

Instead of creating new question, all I needed was one number from a string.

I did this with regex.

def extractInt( String input ) {
  return input.replaceAll("[^0-9]", "")
}

Where input could be this.may.have.number4.com and return 4

I was receiving error from above answer (probably due to my Jenkins version) - For some reason I get this: java.lang.UnsupportedOperationException: spread not yet supported in input.findAll(\d+)*.toInteger() ---- And it says on Jenkins its resolved.

Hope this helps.

答案 3 :(得分:0)

由于input.findAll(/ \ d + /)*。toInteger()无法与Jenkins一起使用。您可以改用它。

def packageVersion = "The package number is 9.2.5847.1275"
def nextversion=packageVersion.findAll( /\d+/ ).collect{ "$it".toInteger() }
nextversion.add(nextversion.pop()+1)
nextversion = nextversion.join('.')
Result: 9.2.5847.1276

答案 4 :(得分:0)

另一个没有RegEx的替代解决方案。它将字符串解析为标记,并将其转换为数字或空值的列表。空值将被删除,最后,仅考虑第一个条目(根据需要)。

def extractNumericData(String response) {
    response.split(' ')
        .collect { it.isFloat() ? Float.parseFloat(it) : null }
        .findAll { it }
        .first()
}

assert 100 == extractNumericData("100 pounds")
assert 173 == extractNumericData("I think 173 lbs")
assert 73 == extractNumericData("73 lbs.")

答案 5 :(得分:-1)

逐行分析String.contains和String.replaceAll(将所有非数字字符序列替换为空格) 然后String.split() 组合非常有用,例如:

if (line.contains("RESULT:")) {
    l = line.replaceAll("[^0-9][^0-9]*"," ")
    a = l.split()
    pCount1 = Integer.parseInt(a[0])
    pCount2 = Integer.parseInt(a[1])
}

String.findAll解决方案更好! 等效:

if (line.contains("RESULT:")) {
    a = line.findAll( /\d+/ )*.toInteger()
    pCount1 = a[0]
    pCount2 = a[1]
}