Golang : Customize scanner.Scanner to treat dash as part of identifier
Putting this down here for my own future reference. Ok, the problem that I'm solving today involved using the text/scanner
package to parse a given input with strings such as beli-belah
, buah-buahan
and jalan-jalan
.
The initial problem is that scanner.Scanner
will break buah-buahan
to buah
and buahan
.
So, how to customize the scanner to treat -
dash as part of the identifier?
Simple, use .IsIdentRune
method to override the default behavior of the scanner
.
For example,
var scn scanner.Scanner
scn.Init(rumiReader)
scn.Whitespace ^= 1<<'\t' | 1<<'\n' | 1<<'\r' | 1<<' ' // don't skip tabs and new lines
// treat leading '-' as part of an identifier ... for word such as buah-buahan, biri-biri
scn.IsIdentRune = func(ch rune, i int) bool {
return ch == '-' && i == 0 || unicode.IsLetter(ch) || unicode.IsDigit(ch) && i > 0 || unicode.IsPunct(ch)
}
If you encounter the same problem as I am, hope this helps!
Reference :
See also : Golang : How to tokenize source code with text/scanner package?
By Adam Ng(黃武俊)
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+7.3k Golang : How to stop user from directly running an executable file?
+32.8k Delete a directory in Go
+30.1k Get client IP Address in Go
+22.3k Golang : Set and Get HTTP request headers example
+12.2k Golang : Forwarding a local port to a remote server example
+14.5k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+19.1k Golang : Fix cannot download, $GOPATH not set error
+25.5k Golang : How to write CSV data to file
+11.3k Golang : Simple file scaning and remove virus example
+5.6k Golang : Launching your executable inside a console under Linux
+6.9k Golang : A simple forex opportunities scanner
+9k Golang : How to check if a string with spaces in between is numeric?