将文件名按多次出现分割

huangapple go评论47阅读模式
英文:

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 = [&quot;dimitri.weqn_good-read.sky.txt&quot;, 
           &quot;yuan.tagine_sold-v1-write.ground.pdf&quot;]

splitted = [
for filename in local.filename: {
type = regex(".+\.(.+)$", filename)[0]
trans = regex(".+\.(.+)\..+$", filename)[0]
operation = regex(".+-([[:alpha:]]+)\.", filename)[0]
rest = regex("^(.+)\-(.+)",filename)[0]
}]
}


</details>



huangapple
  • 本文由 发表于 2023年2月18日 11:20:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定