Split string by multiple delimiters

1,966 views
Skip to first unread message

apoo...@thoughtworks.com

unread,
Jan 8, 2015, 7:16:44 AM1/8/15
to golan...@googlegroups.com
I have a string "hello <format> world" and I need to split it based on multiple characters "<> ". How do I get this ?

One option is: 

strings.FieldsFunc("hello <format> world", func (r rune) bool {
      return r == '<' || r == '>'
})

But this gives ["hello","format","world"].
Is there a way to retain even the delimiters also in this list?

Kevin Malachowski

unread,
Jan 8, 2015, 11:50:20 AM1/8/15
to golan...@googlegroups.com
If your use case really is that simple you could just manually range over you're string and do whatever you need to do yourself. Look at the source for strings.Split and the like for a starting place if you need one.
Message has been deleted

C Banning

unread,
Jan 8, 2015, 3:33:41 PM1/8/15
to golan...@googlegroups.com

speter

unread,
Jan 8, 2015, 4:57:25 PM1/8/15
to C Banning, golang-nuts

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

sab...@gmail.com

unread,
Jan 8, 2015, 5:25:07 PM1/8/15
to golan...@googlegroups.com, clba...@gmail.com
On Thursday, January 8, 2015 at 4:57:25 PM UTC-5, speter wrote:
Very cool!  Works in the playground but locally (both linux & win32) I get "panic: bufio.Scan: 100 empty tokens without progressing"

Jian Zhen

unread,
Jan 8, 2015, 9:41:03 PM1/8/15
to sab...@gmail.com, golan...@googlegroups.com, clba...@gmail.com

András Pálinkás

unread,
Jan 8, 2015, 10:34:42 PM1/8/15
to golan...@googlegroups.com, sab...@gmail.com, clba...@gmail.com
What about regexp.Split?
If speed doesn't matter, that's the easiest, most expressive in code.

András Pálinkás

unread,
Jan 8, 2015, 10:38:49 PM1/8/15
to golan...@googlegroups.com, sab...@gmail.com, clba...@gmail.com
Sorry, this is not good, I've just realized you also want to retain the delimiters.

speter

unread,
Jan 9, 2015, 6:27:13 AM1/9/15
to sab...@gmail.com, golang-nuts, C Banning

Oh, I missed one case... This one should work:
https://play.golang.org/p/UWdQPs3P-W

It seems the bug is triggered on 64bit but not on 32bit (or amd64p32 on which the playground is supposed to run), not quite sure why.

Peter

Michael Monashev

unread,
Jan 9, 2015, 8:24:14 AM1/9/15
to speter
Hi, speter.

> Another take using bufio.Scanner:
> https://play.golang.org/p/gZFodmTXtS

Why did you write:

func (d delimiters) split(data []byte, atEOF bool) (advance int, token []byte, err error) {

and after convert data to string:

i := strings.IndexAny(string(data), string(d))

?

May be better this:
func (d delimiters) split(data string, atEOF bool) (advance int, token []byte, err error) {
i := strings.IndexAny(data, string(d))

or

func (d delimiters) split(data *string, atEOF bool) (advance int, token []byte, err error) {
i := strings.IndexAny(*data, string(d))

?

--
Michael

yy

unread,
Jan 9, 2015, 8:33:40 AM1/9/15
to Michael Monashev, speter
You really want d.split to be a SplitFunc, so you can pass it to
scanner.Split in line 31 (see the documentation of the bufio package).
But if you want to avoid the conversion to string, you can use
bytes.IndexAny instead of strings.IndexAny.


--
- yiyus || JGL .
Reply all
Reply to author
Forward
0 new messages