Regular Expression

Literals in Regular Expression

Literals are the simplest form of pattern matching in regular expressions. They will simply succeed whenever that literal is found.

func Match 

func Match(pattern string, b []byte) (matched bool, err error)

Match reports whether the byte slice b contains any match of the regular expression pattern.

Example Code

package main

import (

func main() {
        // Match() function return a boolean type value 
        // with boolean it limit us from extracting the match string value
	matched, err := regexp.Match(pattern, []byte(`World Hello`))
	// print value return from Match() func
	fmt.Println(matched, err)
	if matched {
	} else {
	   fmt.Println("Not Found")

Go Playground


go doc regexp/syntax

regexp Regular Expression

Regular Expression

regrex package

On-line Regular Expression

Meta Characters

Metacharacters are characters that are interpreted in a special way by a RegEx engine. Here’s a list of metacharacters:

[] . ^ $ * + ? {} () \ |

[] – Square brackets

Square brackets specifies a set of characters you wish to match.

. – Period

A period matches any single character (except newline '\n').

 – Caret

The caret symbol ^ is used to check if a string starts with a certain character.

$ – Dollar

The dollar symbol $ is used to check if a string ends with a certain character.

* – Star

The star symbol * matches zero or more occurrences of the pattern left to it.

+ – Plus

The plus symbol + matches one or more occurrences of the pattern left to it.

? – Question Mark

The question mark symbol ? matches zero or one occurrence of the pattern left to it.

{} – Braces

Consider this code: {n,m}. This means at least n, and at most m repetitions of the pattern left to it.

| – Alternation

Vertical bar | is used for alternation (or operator).

() – Group

Parentheses () is used to group sub-patterns.

\ – Backslash

Backlash \ is used to escape various characters including all metacharacters.

Special Sequences

Special sequences make commonly used patterns easier to write.