简体   繁体   中英

How do I parse the file name below in Groovy ?

文件名的格式为IPCM_ $ date_ $ sequenceNumber.tar.gz例如IPCM_20111012151700_00001.tar.gz在groovy中获取序列号标记的最佳方法是什么?

A very static way would be

txt = 'IPCM_20111012151700_00001.tar.gz'
num = txt[-12..-8]

More dynamically

txt[txt.lastIndexOf('_')+1..txt.indexOf('.')-1]

@Steven: this is your solution but working

raw = 'IPCM_20111012151700_00001.tar.gz'
num = (raw =~ /IPCM_[0-9]+_([0-9]+).tar.gz/)
print num[0][1]

How about this one?

raw = 'IPCM_20111012151700_00001.tar.gz'
seq = raw.tokenize('_').last()-'.tar.gz'

​Or, if you have a random file extension:

​raw = 'IPCM_20111012151700_00011.tar.gz'
seq = raw.tokenize('_').last().tokenize('.').first()

I dont have a groovy compiler on me, but try:

def seqNum = (raw ~= /IPCM_[0-9+]_([0-9+]).tar.gz/)[0]

It is probably a bit wrong, but you should get the gist. (Use regexp matching)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM