0

I have a two strings like this

mystr = "xyz/10021abc/f123"
mystr2 = "abc/10021abd/c222"

I want to extract 10021abc and 10021abd. I came up with

r = regexp.MustCompile(`(?:xyz\/|abc\/)(.+)\/`)

But when I want to extract the match using this:

fmt.Println(r.FindString(mystr))

It returns the entire string. How should I change my regex?

drdot
  • 3,215
  • 9
  • 46
  • 81
  • You already have it, it is in the first capture group. See https://stackoverflow.com/questions/30483652/how-to-get-capturing-group-functionality-in-go-regular-expressions – The fourth bird Sep 16 '22 at 07:13

2 Answers2

2

You can use FindStringSubmatch.

var re = regexp.MustCompile(`(?:xyz\/|abc\/)(.+)\/`)
var s1 = "xyz/10021abc/f123"
var s2 = "abc/10021abd/c222"

fmt.Println(re.FindStringSubmatch(s1)[1])
fmt.Println(re.FindStringSubmatch(s2)[1])

https://go.dev/play/p/C93DbfzVv3a

mkopriva
  • 35,176
  • 4
  • 57
  • 71
  • This program crash when there is no match – drdot Sep 16 '22 at 14:43
  • Of course it does. Any Go slice index expression will crash if there isn't enough elements in the slice. The standard approach is to check the slice's length *before* indexing. But that was not your question. – mkopriva Sep 16 '22 at 14:50
  • I already know how to do this. My Q is how to change the regex – drdot Sep 16 '22 at 14:50
  • It is a regex skill Q rather than a go programming Q if that makes sense ;) – drdot Sep 16 '22 at 14:51
  • I am happy to take this answer if you think updating regex to just match the substring I need is too hard – drdot Sep 16 '22 at 14:52
  • @drdot Well your regex matches and captures the substring for the two input examples you've shown, only your method of retrieving the match was wrong. If your question is about how to use regex to match infinite number of variations of the general structure in the two input string then why not use something like the following? https://go.dev/play/p/CdsXlvKVMph – mkopriva Sep 16 '22 at 14:56
  • @drdot for completness sake: https://go.dev/play/p/ws7UfBD98nP – mkopriva Sep 16 '22 at 14:57
  • Thank you for the clarification. I am just curious if there a way to have only one matching group and match the middle string "10021abc" and not have two matching group? – drdot Sep 16 '22 at 15:03
  • @drdot I do not know if an RE can be written, using Go, that would match your requirements. – mkopriva Sep 16 '22 at 15:21
1

You could use a regex replacement here:

var mystr = "xyz/10021abc/f123"
var re = regexp.MustCompile(`^.*?/|/.*$`)
var output = re.ReplaceAllString(mystr, "")
fmt.Println(output)  // 10021abc
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360