Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

regexp question

59 views
Skip to first unread message

Jochen Voss

unread,
Feb 10, 2023, 7:52:34 AM2/10/23
to golang-nuts
Dear all,

What happens if a group in a regular expression matches repeatedly, via the * operator?  Experimentally I found that FindStringSubmatch then returns the text of the last match.  Is this guaranteed somewhere (I didn't find anything about this on https://pkg.go.dev/regexp )?  Also, is there an easy way to get the other matched strings?


regexp: `A\s*(?:([a-z]+)\s*)*B`
applied to: "one A two three four B five"
the inner group gives me "four"

Is FindStringSubmatch guaranteed to return the "four"? 
Is there a way to also get the matched "two" and "three"?

Many thanks,
Jochen

Peter Galbavy

unread,
Feb 10, 2023, 11:09:04 AM2/10/23
to golang-nuts

Jochen Voss

unread,
Feb 10, 2023, 12:40:15 PM2/10/23
to golang-nuts
Hi Peter,

I don't think this is the solution: https://go.dev/play/p/Ti7UEGd1BVE .
I think "All" means that it can deal with several copies of the regexp, rather than returning all matches of the group.

All the best,
Jochen

Reply all
Reply to author
Forward
0 new messages