Products

Solutions

Resources

Search for API Security Tests

Go RegEx Tester

Go RegEx Tester

Regex Tester Online tools to learn, build & test Regular Expression (RegEx/RegExp)
0 match
/
/ gm
Possible security issues
Explanation
Match information
Akto.io

Regular Expression - Documentation

Regular Expression - Documentation

Introduction

Go regular expressions provide a powerful tool for string matching and manipulation. Leveraging the regexp package in Go, developers can efficiently perform tasks like string searching, parsing, and replacing. Go's regex engine is built for speed and efficiency, making it suitable for various applications.

Core Constructs of Go Regex

Go regex uses several constructs to match and manipulate strings:

Metacharacters

  • .: Matches any character except newline.

  • ^: Matches the start of a string.

  • $: Matches the end of a string or just before the newline at the end.

  • |: Acts as a logical OR operator.

Character Classes

  • [abc]: Matches any one of the characters a, b, or c.

  • [^abc]: Negates the set; matches any character except a, b, or c.

  • [a-zA-Z]: Matches any letter from a to z or A to Z.

Predefined Character Classes

  • \\d: Matches any digit.

  • \\D: Matches any non-digit.

  • \\s: Matches any whitespace character.

  • \\S: Matches any non-whitespace character.

  • \\w: Matches any word character (alphanumeric and underscore).

  • \\W: Matches any non-word character.

Quantifiers

  • ``: Zero or more occurrences.

  • +: One or more occurrences.

  • ?: Zero or one occurrence, making it optional.

  • {n}: Exactly n occurrences.

  • {n,}: At least n occurrences.

  • {n,m}: Between n and m occurrences.

Special Constructs

  • (abc): Captures the group abc.

  • (?:abc): Non-capturing version of regular parentheses.

  • (?=abc): Positive lookahead assertion for abc.

  • (?!abc): Negative lookahead assertion for abc.

Anchors and Boundaries

  • \\b: Word boundary.

  • \\B: Non-word boundary.

Greedy and Non-Greedy Matching

  • Go regex patterns follow greedy matching by default, which can be controlled using quantifiers.

Go Regular Expressions Examples

Example 1: Email Validation


package main

import (
    "fmt"
    "regexp"
)

func main() {
    var emailRegex = regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\\\.[a-zA-Z]{2,}$`)
    email := "user@example.com"
    fmt.Println("Email Valid:", emailRegex.MatchString(email))
}

Example 2: Password Strength Check


package main

import (
    "fmt"
    "regexp"
)

func main() {
    var passwordRegex = regexp.MustCompile(`(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}`)
    password := "Aa123456!"
    fmt.Println("Password Strong:", passwordRegex.MatchString(password))
}

Example 3: Extracting Words from a String


package main

import (
    "fmt"
    "regexp"
)

func main() {
    var wordRegex = regexp.MustCompile(`\b\w+\b`)
    text := "Regex is #1 at pattern matching!"
    matches := wordRegex.FindAllString(text, -1)
    for _, match := range matches {
        fmt.Println("Found:", match)
    }
}

Practical Tips for Go Regular Expressions

  1. Pre-compile regex patterns using regexp.MustCompile for efficient reuse, especially in performance-critical applications.

  2. Utilize Go's regex methods like MatchString, FindString, FindAllString appropriately based on the requirement.

  3. Test your regex patterns with various input scenarios to ensure they work as expected.

  4. For complex patterns, consider breaking them down into smaller, understandable segments or use verbose mode with comments.

  5. Leverage online regex testers, such as regex101 or Akto regex tester, for debugging and optimizing your regex patterns.

  6. Be aware of the performance implications of regex, especially with large input strings or in high-throughput scenarios.

  7. Go’s regex engine doesn't support lookbehind assertions, so plan your patterns accordingly.

  8. Regular expressions are case-sensitive by default. Use the (?i) flag for case-insensitive matching.

  9. Remember to escape special characters when they are meant to be matched literally in patterns.

Understanding and applying these constructs and tips allows developers to effectively utilize regular expressions in Go. For validating complex and varied patterns, Akto's regex validator is a valuable tool for testing and ensuring accuracy.

Frequently asked questions

What is Go Regex Tester?

How does Go's regex engine differ from other languages?

Can I test multi-line regex patterns in Go Regex Tester?

Does the Go Regex Tester support UTF-8 encoded text?

How can I optimize regex performance in Go?

Go RegEx Tester

/

/

Go RegEx Tester