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 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c