A
A
Adatan2019-12-26 22:50:35
go
Adatan, 2019-12-26 22:50:35

How to get the desired regexp result by pattern from text, not bool?

Given text:
`From:` <@317989885321674753>
`To:` <@163995016027439106>
`Reason:` <@!163995016027439106> ....
You need to use regular expressions to get from it only the first one similar to "317989885321674753" it's just some ID is always the same length (18) and always digits.
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Pavlyuk, 2019-12-27
@Adatan

Like this:
https://play.golang.org/p/cPnYojRRPP3

package main

import (
  "fmt"
  "regexp"
)

func main() {
  text := "`От:` <@317989885321674753>\n`На:` <@163995016027439106>\n`Причина:` <@!163995016027439106>"

  re := regexp.MustCompile(`\<\@\!?(\d{18})\>`)
  matches := re.FindAllStringSubmatch(text, -1)
  for _, match := range matches {
    if len(match) > 1 {
      fmt.Println(match[1])
    }
  }
}

I
index0h, 2019-12-26
@index0h

https://golang.org/pkg/regexp/#example_Regexp_Find...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question