英文:
get the xlsx cell data using the the golang regexp?
问题
我使用正则表达式从.xlsx文件中获取数据,但我对正则表达式不熟悉。有人可以帮助我吗?
问题:
-
如何获取字符串{{range .txt}},并去掉标签"
..."? -
如何从
r="3"
中获取"3",并从", , ..."中获取"A3,B3,C3..."?
提前感谢!
英文:
I use the regexp expression to get the data from .xlsx file. but i am poor and a newer in regexp. Anyone could help me?
package main
import (
"fmt"
"regexp"
)
func main() {
input := `
<sheetData>
<row r="2" spans="1:15">
<c r="A2" s="5" ><v>{{range .txt}}</v></c>
<c r="B2" s="5" t="s"><v>1</v></c>
<c r="C2" s="5" t="s"><v>2</v></c>
<c r="D2" s="5" t="s"><v>3</v></c>
<c r="E2" s="5" />
<c r="K2" s="6" t="s"><v>21</v></c>
</row>
<row r="3" spans="1:15">
<c r="A3" s="5" t="s"><v>0</v></c>
<c r="B3" s="5" t="s"><v>1</v></c>
<c r="C3" s="5" t="s"><v>2</v></c>
<c r="D3" s="5" t="s"><v>3</v></c>
<c r="E3" s="5" />
<c r="K3" s="6" t="s"><v>21</v></c>
</row>
</sheetData>`
r := regexp.MustCompile(`<row[^>]*?r="(\d+)"[^>].*?>.*?[(<v>(.*?)<\/v>.*?)]<\/row>`)
r2 := regexp.MustCompile(`<v>(.*?)</v>`)
row:=r.FindAllString(input,-1)
for _,v:=range row {
fmt.Println(r.ReplaceAllStringFunc(v, func(m string) string {
match:=r2.FindAllString(v,-1)
for kk,vv:=range match {
fmt.Println(kk,vv)
fmt.Println(r2.ReplaceAllString(v, ""))
}
}))
}
}
Question:
-
How to get the string {{range .txt}} ,and throw off the tag"<row><c>..."
-
How to get the "3" from
r="3"
,and get the "A3,B3,C3..." from the "<c r="A3",<c r="B3",<c r="C3"...."
Thanks in advance!
答案1
得分: 3
我认为regexp
不适合这个任务。尝试使用xml:
import "encoding/xml"
// 可能可以为这些选择更好的名称。
type C struct {
XMLName xml.Name `xml:"c"`
V string `xml:"v"`
R string `xml:"r,attr"`
}
type Row struct {
XMLName xml.Name `xml:"row"`
C []C `xml:"c"`
}
type Result struct {
XMLName xml.Name `xml:"sheetData"`
Row []Row `xml:"row"`
}
v := Result{}
err := xml.Unmarshal([]byte(input), &v)
if err != nil {
fmt.Printf("error: %v", err)
return
}
for _, r := range v.Row {
for _, c := range r.C {
fmt.Printf("%v %v\n", c.V, c.R)
}
}
这将打印:
{{range .txt}} A2
1 B2
2 C2
3 D2
...
英文:
I think regexp
is the wrong tool for this job. Try xml:
import "encoding/xml"
// Could probably pick better names for these.
type C struct {
XMLName xml.Name `xml:"c"`
V string `xml:"v"`
R string `xml:"r,attr"`
}
type Row struct {
XMLName xml.Name `xml:"row"`
C []C `xml:"c"`
}
type Result struct {
XMLName xml.Name `xml:"sheetData"`
Row []Row `xml:"row"`
}
v := Result{}
err := xml.Unmarshal([]byte(input), &v)
if err != nil {
fmt.Printf("error: %v", err)
return
}
for _, r := range v.Row {
for _, c := range r.C {
fmt.Printf("%v %v\n", c.V, c.R)
}
}
This will print:
{{range .txt}} A2
1 B2
2 C2
3 D2
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论