我们的代码当中有很多很多如下的代码:
UarrMain[0] := AsMainType(edLayer, myStr, 'Layer', '结构层名称');
UarrMain[1] := AsMainType(edKind, myStr, 'Kind', '稳定剂种类');
.......
UArrSub[0] := AsSubType(myInt, True, 'seqnum', '序号');
UArrSub[1] := AsSubType(myStr, False, 'BoxNumber', '试验盒号');
UArrSub[2] := AsSubType(myFloat, False, 'BoxWeigth', '盒的质量');
.....
我现在需要提取第三个,第四个参数。
放上来,下次电脑坏就不用重新写了。工作当中用到的,可以减轻工作量。
一边学一边写的,比较手生,翻了很多次文档才写出来。关键是正则,当然没有考虑所有的情况。
这门语言对GUI支持的不好,因为是面向服务器的开发。就胡乱弄了个B/S的,代码如下:
package main
import
(
"io"
"fmt"
"regexp"
"strings"
"net/http"
)
const(
SQL = "EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'AAAA', @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'CCCC', @level2type=N'COLUMN',@level2name=N'BBBB'"
SQL2 = "EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'AAAA', @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'CCCC'"
html =
`<html>
<head><title>提取<a/title></head>
<body><form method = 'post'>
TableName : <input type = "text" name = "tablename"></input> qs_Table 中的 TableName,比如 TestRecordC21 <br></br>
表格名称 : <input type = "text" name = "tablename2"></input>比如 石料试验记录 <br></br>
<textarea rows="20" cols="120" name = "target"></textarea><br></br>
<input type = "submit" value = "Submit"></input>
%s
</form>
</body>
</html>`
)
func mySearch(str string) (result string) {
result = ""
regSub := regexp.MustCompile(`(?i)AsSubType\s*\(\w+\s*\,\s*(True|False)\s*\,\s*\'\s*(\w+)\s*\'\s*\,\s*\'\s*(.*?)\s*\'`)
regMain := regexp.MustCompile(`(?i)AsMainType\s*\(\w+\s*\,\s*(\w+)\s*\,\s*\'\s*(\w+)\s*\'\s*\,\s*\'\s*(.*?)\s*\'`)
resultSub := regSub.FindAllStringSubmatch(str, -1)
resultMain := regMain.FindAllStringSubmatch(str, -1)
for _, V := range resultSub {
result = result + strings.Replace(strings.Replace(SQL, "AAAA", V[3],-1), "BBBB", V[2], -1) +
"<br></br>" + " GO " + "<br></br>"
}
for _, V := range resultMain {
result = result + strings.Replace(strings.Replace(SQL, "AAAA", V[3],-1), "BBBB", V[2], -1) +
"<br></br>" + " GO " + "<br></br>"
}
return
}
func Analyze(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
io.WriteString(w, strings.Replace(html, "%s", "", -1))
return
}
if r.Method == "POST"{
str := r.FormValue("target")
tablename := r.FormValue("tablename")
tablename2 := r.FormValue("tablename2")
if tablename == "" {
io.WriteString(w, strings.Replace(html, "%s", "<div border = '1'>" +
"TableName 没有填写 " +"</div>", -1))
return
}
if tablename2 == "" {
io.WriteString(w, strings.Replace(html, "%s", "<div border = '1'>" +
"表格名称 没有填写 " +"</div>", -1))
return
}
if str == "" {
io.WriteString(w, strings.Replace(html, "%s", "<div border = '1'>" +
"查找内容 没有填写 " +"</div>", -1))
return
}
SQLCMD := strings.Replace(html, "%s", "<div border = '1'>" +
strings.Replace(mySearch(str), "CCCC", tablename, -1) +"</div>", -1)
SQLCMD = SQLCMD + strings.Replace(strings.Replace(SQL2, "AAAA", tablename2, -1), "CCCC", tablename, -1)
io.WriteString(w, SQLCMD)
}
}
func main(){
fmt.Println("Open http://localhost:8888/extract")
http.HandleFunc("/extract", Analyze)
err := http.ListenAndServe(":8888", nil)
if err != nil {
fmt.Println(err.Error())
}
}
©著作权归作者所有:来自51CTO博客作者huanghongqiao的原创作品,如需转载,请注明出处,否则将追究法律责任
共同学习,写下你的评论
评论加载中...
作者其他优质文章