Golang regexp/syntax.IsWordChar() function example

package regexp/syntax

Golang regexp.syntax.IsWordChar() function usage example.

NOTE : These assertions are ASCII-only: the word characters are [A-Za-z0-9_].

 package main

 import (
  "fmt"
  "regexp/syntax"
 )

 func main() {

  word1 := []rune("alpha")
  word2 := []rune("吃")
  word3 := []rune("1234")
  word4 := []rune(" $#$^@#$ ")

  ok := syntax.IsWordChar(word1[0])

  fmt.Printf("%v is a word ? : %v \n", string(word1), ok)

  ok = syntax.IsWordChar(word2[0])

  fmt.Printf("%v is a word ? : %v \n", string(word2), ok)

  ok = syntax.IsWordChar(word3[0])

  fmt.Printf("%v is a word ? : %v \n", string(word3), ok)

  ok = syntax.IsWordChar(word4[0])

  fmt.Printf("%v is a word ? : %v \n", string(word4), ok)

 }

Output :

alpha is a word ? : true

吃 is a word ? : false

1234 is a word ? : true

$#$^@#$ is a word ? : false

Reference :

http://golang.org/pkg/regexp/syntax/#IsWordChar

Advertisement