英文:
Split the filename with multiple occurence
问题
{
type = txt
trans = sky
operation = read
rest = dimitri.weqn_good
},
{
type = pdf
trans = ground
operation = write
rest = yuan.tagine_sold-v1
}
英文:
Trying to split file name into multiple variables with Delimiter
Filename example :
dimitri.weqn_good-read.sky.txt
yuan.tagine_sold-v1-write.ground.pdf
Expected
{
type = txt
trans = sky
operation = read
rest = dimitri.weqn_good
},
{
type = pdf
trans = ground
operation = write
rest = yuan.tagine_sold-v1
Tried with this
operation = write
rest = ${split("-", file)[0]}
this is failing with multiple delimeters "-"
actual
rest = "yuan.tagine_sold"
expected = "yuan.tagine_sold-v1"
答案1
得分: 2
您始终可以使用正则表达式:
本地{
文件名 = ["dimitri.weqn_good-read.sky.txt",
"yuan.tagine_sold-v1-write.ground.pdf"]
分割 = [
对于本地文件名的正则表达式: {
类型 = 正则表达式(".+\.(.+)$", 文件名)[0]
翻译 = 正则表达式(".+\.(.+)\..+$", 文件名)[0]
操作 = 正则表达式(".+-([[:alpha:]]+)\.", 文件名)[0]
其余 = 正则表达式("^(.+)\-(.+)",文件名)[0]
}]
}
<details>
<summary>英文:</summary>
You can always use regular expressions:
locals{
filename = ["dimitri.weqn_good-read.sky.txt",
"yuan.tagine_sold-v1-write.ground.pdf"]
splitted = [
for filename in local.filename: {
type = regex(".+\.(.+)$", filename)[0]
trans = regex(".+\.(.+)\..+$", filename)[0]
operation = regex(".+-([[:alpha:]]+)\.", filename)[0]
rest = regex("^(.+)\-(.+)",filename)[0]
}]
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论