[英]Extract numeric data from string in 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
}
}
您可以使用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 ]
把這個放在這里給也需要這個的人。
我只需要一個字符串中的一個數字,而不是創建新問題。
我用正則表達式做到了這一點。
def extractInt( String input ) {
return input.replaceAll("[^0-9]", "")
}
輸入可能是this.may.have.number4.com
並返回4
我從上面的答案中收到錯誤(可能是由於我的 Jenkins 版本)-出於某種原因,我得到了這個: java.lang.UnsupportedOperationException: spread not yet supported in input.findAll(\\d+)*.toInteger()
----它在Jenkins 上說它已解決。
希望這可以幫助。
添加以下方法后, numbersFilter
,通過 metaClass ,您可以按如下方式調用它:
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();
}
}
}
由於 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
另一種替代解決方案,沒有 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.")
當逐行解析 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]
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.