代码之家  ›  专栏  ›  技术社区  ›  Leon

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

  •  0
  • Leon  · 技术社区  · 6 年前

    我在使用正则表达式时遇到了一个问题,需要解决两个问题,从简单到复杂。 首先是使用正则表达式来匹配字符串,然后它应该从消息中检索一些子字符串。

    就像我有根绳子,那是

    “在现在的聊天室里:你今天吃什么?(此消息由Sharon编辑,该消息于2018-11-10 21:00:00从Leon发送)

    “在现在的聊天室里:嘿,伙计,你喜欢歌朗吗?(此消息由Leon编辑,消息由Mike于2018-01-10 10:00:59发送)

    在上面的消息中,某些部分将不会更改 就像“在当前聊天室中:”和“此邮件由……编辑”,此邮件将发送到…从……”

    当我遇到这种信息时,这被认为是“编辑通知” 我需要过滤用这个结构编译的所有消息。

    我写的是

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

    我知道这有点愚蠢,但至少能起作用

    当我运行它时,结果显示它是真的。

    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 ))
    

    到现在为止我觉得还可以

    第二步是检索内容、编辑器、时间和原始发送者。

    我所做的是替换了第一部分,即“在当前聊天室中”。 然后字符串变为

    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)"
    

    从绳子的末端,我把最后一根绳子剪断,这样我就可以把“利昂”取出来。

    //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 "
    

    然后在最后一段之后剪断绳子以获得时间。

    //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 "
    

    最后一步是检索编辑器。

    我认为这个方法非常愚蠢,我已经搜索了一些示例,比如使用regexp检索组件 Golang: extract data with Regex

    但这是一个有点复杂的情况,我认为检索我编写的组件的方法是非常难看的。

    请问是否有直接使用正则表达式获取组件的方法?

    对于通知消息,

    “在当前聊天室中:”不更改,已编辑邮件的组件将更改,括号内的内容将只更改编辑器(Sharon)、时间(2018-11-10 21:00:00)和发件人(Leon),括号内的其他部分不会更改为

    (此消息由XXXX编辑,XXXX于XXXX发送)

    1 回复  |  直到 6 年前
        1
  •  1
  •   Kishore Karunakaran    6 年前

    让我试着理解您的问题,在给定的输入字符串中,您需要查找编辑器和发送者名称,还需要提取日期和时间。

    首先,您可以有两个regex,一个用于匹配名称,另一个用于日期和时间,您可以这样做

    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])
    }
    

    产量 :

    编辑人=莎伦

    发件人=Leon

    2018年11月10日21:00:00

        2
  •  0
  •   AJR    6 年前

    我不能发表评论,所以我必须把这个放在这里… 你研究过Regex捕获组吗?

    How to get capturing group functionality in Golang regular expressions?