match := sampleRegexp.Match([]byte(\"some_string\"))<\/code><\/pre>\n\n\n\nWe will see these two functions in action later in the examples.<\/p>\n\n\n\n
Now let’s see a simple program for Dot ‘.’ character<\/p>\n\n\n\n
package main\n\nimport (\n\t\"fmt\"\n\t\"regexp\"\n)\n\nfunc main() {\n\tsampleRegexp := regexp.MustCompile(\".\")\n\n\tmatch := sampleRegexp.Match([]byte(\"a\"))\n\tfmt.Printf(\"For a: %t\\n\", match)\n\n\tmatch = sampleRegexp.Match([]byte(\"b\"))\n\tfmt.Printf(\"For b: %t\\n\", match)\n\n\tmatch = sampleRegexp.Match([]byte(\"ab\"))\n\tfmt.Printf(\"For ab: %t\\n\", match)\n\n\tmatch = sampleRegexp.Match([]byte(\"\"))\n\tfmt.Printf(\"For empty string: %t\\n\", match)\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nFor a: true\nFor b: true\nFor ab: true\nFor empty string: false<\/code><\/pre>\n\n\n\nIn the above program, we have a simple regex containing only one dot character.<\/p>\n\n\n\n
sampleRegexp := regexp.MustCompile(\".\")<\/code><\/pre>\n\n\n\nIt matches below characters and string.<\/p>\n\n\n\n
a\nb\nab<\/code><\/pre>\n\n\n\nIt matches ab <\/strong>because by default the regex doesn’t do the match the full string unless we use the anchor characters (Caret and Dollar character). That is why it matches the first character ‘a’ in ‘ab’ and reports a match.<\/p>\n\n\n\nIt doesn’t match an empty string.<\/p>\n\n\n\n
Let’s see another example where we have two dots in the regex.<\/p>\n\n\n\n
package main\n\nimport (\n\t\"fmt\"\n\t\"regexp\"\n)\n\nfunc main() {\n\tsampleRegexp := regexp.MustCompile(\"..\")\n\tmatch := sampleRegexp.Match([]byte(\"ab\"))\n\tfmt.Printf(\"For ab: %t\\n\", match)\n\n\tmatch = sampleRegexp.Match([]byte(\"ba\"))\n\tfmt.Printf(\"For ba: %t\\n\", match)\n\n\tmatch = sampleRegexp.Match([]byte(\"abc\"))\n\tfmt.Printf(\"For abc: %t\\n\", match)\n\n\tmatch = sampleRegexp.Match([]byte(\"a\"))\n\tfmt.Printf(\"For a: %t\\n\", match)\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nFor ab: true\nFor ba: true\nFor abc: true\nFor a: false<\/code><\/pre>\n\n\n\nIn the above program, we have a simple regex containing two dots.<\/p>\n\n\n\n
sampleRegexp := regexp.MustCompile(\"..\")<\/code><\/pre>\n\n\n\nIt will match any given string which has at least two characters as a substring.<\/p>\n\n\n\n
That is why it gives a match for<\/p>\n\n\n\n
ab\nba\nabc<\/code><\/pre>\n\n\n\nand doesn’t give a match for<\/p>\n\n\n\n
a<\/code><\/pre>\n\n\n\nThe dot ‘.’<\/strong> as we mentioned before as well doesn’t match the new line. But the default behavior can be changed by adding a set of flags to the beginning of the regular expression. The flag we need to add to the beginning of regex is:<\/p>\n\n\n\n(?s)<\/code><\/pre>\n\n\n\nLet’s see a program for the same<\/p>\n\n\n\n
package main\n\nimport (\n\t\"fmt\"\n\t\"regexp\"\n)\n\nfunc main() {\n\tsampleRegexp := regexp.MustCompile(\".\")\n\n\tmatch := sampleRegexp.Match([]byte(\"\\n\"))\n\tfmt.Printf(\"For \\\\n: %t\\n\", match)\n\n\tsampleRegexp = regexp.MustCompile(\"(?s).\")\n\n\tmatch = sampleRegexp.Match([]byte(\"\\n\"))\n\tfmt.Printf(\"For \\\\n: %t\\n\", match)\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nFor \\n: false\nFor \\n: true<\/code><\/pre>\n\n\n\nsampleRegexp := regexp.MustCompile(\".\")<\/code><\/pre>\n\n\n\nand<\/p>\n\n\n\n
sampleRegexp = regexp.MustCompile(\"(?s).\")<\/code><\/pre>\n\n\n\nIn the second regex, we have added the additional flag. That is why it gives a match for a new line while the first regex without a flag doesn’t give a match. <\/p>\n\n\n\n
<\/span>Using Dot as a literal character<\/strong><\/span><\/h2>\n\n\n\nIf you want to use Dot ‘.’<\/strong> as a literal character, we need to escape it with a backslash. Once escaped it will match a literal dot character. For example, if we want to match the literal below string or text<\/p>\n\n\n\na.b<\/code><\/pre>\n\n\n\nThen the regex for the same will be<\/p>\n\n\n\n
a\\.b<\/code><\/pre>\n\n\n\nHere is the program for the same<\/p>\n\n\n\n
package main\n\nimport (\n\t\"fmt\"\n\t\"regexp\"\n)\n\nfunc main() {\n\tsampleRegexp := regexp.MustCompile(\"a\\\\.b\")\n\n\tmatch := sampleRegexp.Match([]byte(\"a.b\"))\n\n\tfmt.Printf(\"For a.b string: %t\\n\", match)\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nFor a.b string: true<\/code><\/pre>\n\n\n\n<\/span>Dot character inside a character class<\/strong><\/span><\/h2>\n\n\n\nDot or \u2018.\u2019<\/strong> is treated as a literal character inside the square brackets or character class. It doesn\u2019t need to be escaped inside that. Let\u2019s see a working program for the same as well<\/p>\n\n\n\npackage main\n\nimport (\n\t\"fmt\"\n\t\"regexp\"\n)\n\nfunc main() {\n\tsampleRegexp := regexp.MustCompile(\"[.]\")\n\tmatch := sampleRegexp.Match([]byte(\".\"))\n\n\tfmt.Println(match)\n\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\ntrue<\/code><\/pre>\n\n\n\nAlso, check out our Golang advance tutorial Series \u2013\u00a0Golang Advance Tutorial<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Table of Contents OverviewMatchCompile FunctionMatch MethodUsing Dot as a literal characterDot character inside a character class Overview Dot ‘.’ character is one of the most commonly used metacharacters in the regular expression….<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[3,4],"class_list":["post-5732","post","type-post","status-publish","format-standard","hentry","category-tech","tag-go","tag-golang"],"yoast_head":"\n
Golang Regex: Understanding dot '.' character - Welcome To Golang By Example<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n