dongmanzui8486 2019-04-17 02:09
浏览 83
已采纳

是否可以从golang中的字符串(复杂字符串)中检索子字符串

I met a problem in using regular expression, two questions need to be solved, from simple to complex. Firstly is to use regular expression to match the string, after that it should retrieve some substrings from the message.

like I have a string, which is

"In current chatting room: what do you eat for today? (This message is edited by Sharon, the message is sent on 2018-11-10 21:00:00 from Leon)"

"In current chatting room: Hey mate, do you like golang? (This message is edited by Leon, the message is sent on 2018-01-10 10:00:59 from Mike)"

In the above message, some part will not change like "In current chatting room:" and "This message is edited by ..., the message is sent on ... from ..."

When I met this kind of message, this is considered as "Editing Notice" I need to filter all the message that compile with the structure.

What I write is

var testRgx = regexp.MustCompile(`^In current chatting room: .* \(This message is edited by .*, the message is sent on .* from .*\)$`)

I know it is a little stupid, but at least could work

and when I run it, the result shows it is true.

sample := "In current chatting room: what do you eat for today? I input some shit (sdfhjskdfjksljhfdsjkdf) can you detect this? (This message is edited by Sharon, the message is sent on 2018-11-10 21:00:00 from Leon)"
fmt.Println(testRgx.MatchString(sample ))

Until now I think it is fine

The second step is to retrieve the content, the editor, the time and the original sender.

What I did is I replace the first part, which is "In current chatting room: " and then the string is changed to

changedString := "what do you eat for today? I input some shit (sdfhjskdfjksljhfdsjkdf) can you detect this? (This message is edited by Sharon, the message is sent on 2018-11-10 21:00:00 from Leon)"

And from the end of string, I cut the string after the last from, so I could fetch "Leon" out.

//after cut after from
cutString := "what do you eat for today? I input some shit (sdfhjskdfjksljhfdsjkdf) can you detect this? (This message is edited by Sharon, the message is sent on 2018-11-10 21:00:00 "

Then cut the string after the last on to get the time.

//after cut after on
cutString := "what do you eat for today? I input some shit (sdfhjskdfjksljhfdsjkdf) can you detect this? (This message is edited by Sharon, the message is sent "

Then the last step is to retrieve the editor out.

I think this method is quite stupid, I have searched some example like retrieve component using regexp Golang: extract data with Regex

but this is a little complex case, I think the method to retrieve component I written is quite ugly.

Can I please ask whether there is a way to directly use regular expression to fetch the components?

For the notice message,

"In current chatting room: " will not change, the component of edited message will change, and the content inside bracket will only change the editor (Sharon), time (2018-11-10 21:00:00) and sender (Leon), other part in the bracket will not change like

(This message is edited by xxxxx, the message is sent on xxxx from xxxx)

  • 写回答

2条回答 默认 最新

  • dsfds2343 2019-04-17 03:13
    关注

    Let me try to understand your problem, In the given input string, you want to find editor and sender name and also you want to extract date and time.

    As a start, You can have two regex one for matching names and one more for date and time, You could do something like this

    namesRegex, _ := regexp.Compile("by\\s(.*?),(.*?)\\s*from\\s*(.*?)\\)")
    dateTimeRegex, _ := regexp.Compile("(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})")
    input := "In current chatting room: what do you eat for today? (This message is edited by Sharon, the message is sent on 2018-11-10 21:00:00 from Leon)"
    if namesRegex.MatchString(input) {
        res := namesRegex.FindStringSubmatch(input)
        fmt.Println("Edited by = ", strings.TrimSpace(res[1]))
        fmt.Println("From = ", strings.TrimSpace(res[3]))
    }
    if dateTimeRegex.MatchString(input) {
        res := dateTimeRegex.FindAllString(input, 1)
        fmt.Println(res[0])
    }
    

    Output:

    Edited by = Sharon

    From = Leon

    2018-11-10 21:00:00

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?