Skip to main content

> Regex Patterns_

Predefined regex patterns for common validation scenarios.

Regex Dependency

Regex validation requires PCRE2. If compiled without regex support (-Dregex=false), these patterns return stub implementations that report "Regex support disabled".

// Usage

#include "argus/regex.h"

OPTION_STRING('e', "email", HELP("Email address"),
VALIDATOR(V_REGEX(ARGUS_RE_EMAIL)))

// Network Patterns

IP Addresses

PatternValidatesExample
ARGUS_RE_IPV4IPv4 addresses192.168.1.1
ARGUS_RE_IPV6IPv6 addresses2001:db8::1
OPTION_STRING('i', "ip", HELP("Server IP address"),
VALIDATOR(V_REGEX(ARGUS_RE_IPV4)))

Network Infrastructure

PatternValidatesExample
ARGUS_RE_DOMAINDomain names (FQDN)example.com
ARGUS_RE_PORTPort numbers (1-65535)8080
OPTION_STRING('d', "domain", HELP("Domain name"),
VALIDATOR(V_REGEX(ARGUS_RE_DOMAIN)))

URLs

PatternValidatesExample
ARGUS_RE_URLAny protocol URLhttps://example.com/path
ARGUS_RE_HTTPHTTP(S) URLs onlyhttp://example.com
OPTION_STRING('u', "url", HELP("Website URL"),
VALIDATOR(V_REGEX(ARGUS_RE_URL)))

OPTION_STRING('a', "api", HELP("API endpoint"),
VALIDATOR(V_REGEX(ARGUS_RE_HTTP)))

// Communication Patterns

Email

PatternValidatesExample
ARGUS_RE_EMAILBasic email formatuser@example.com
ARGUS_RE_EMAIL_STRICTRFC-compliant emailuser.name+tag@example.com
OPTION_STRING('e', "email", HELP("Email address"),
VALIDATOR(V_REGEX(ARGUS_RE_EMAIL)))

OPTION_STRING('c', "contact", HELP("Contact email (strict)"),
VALIDATOR(V_REGEX(ARGUS_RE_EMAIL_STRICT)))

// Date and Time Patterns

PatternValidatesExample
ARGUS_RE_ISO_DATEISO date (YYYY-MM-DD)2024-03-15
ARGUS_RE_ISOTIMEISO datetime2024-03-15T14:30:00
OPTION_STRING('d', "date", HELP("Date (YYYY-MM-DD)"),
VALIDATOR(V_REGEX(ARGUS_RE_ISO_DATE)))

OPTION_STRING('t', "datetime", HELP("DateTime (ISO format)"),
VALIDATOR(V_REGEX(ARGUS_RE_ISOTIME)))

// Security Patterns

PatternValidatesExample
ARGUS_RE_USERUsername (3-20 chars)user_123
ARGUS_RE_PASSWDBasic passwordpassword123
ARGUS_RE_UUIDUUID v4 format550e8400-e29b-41d4-a716-446655440000
OPTION_STRING('u', "username", HELP("Username"),
VALIDATOR(V_REGEX(ARGUS_RE_USER)))

OPTION_STRING('p', "password", HELP("Password"),
VALIDATOR(V_REGEX(ARGUS_RE_PASSWD)))

// File System Patterns

PatternValidatesExample
ARGUS_RE_UNIX_PATHUnix absolute paths/path/to/file
ARGUS_RE_FILENAMEFilename with extensiondocument.pdf
OPTION_STRING('f', "file", HELP("File path"),
VALIDATOR(V_REGEX(ARGUS_RE_UNIX_PATH)))

OPTION_STRING('n', "name", HELP("Filename"),
VALIDATOR(V_REGEX(ARGUS_RE_FILENAME)))

// Numbers and Formats

PatternValidatesExample
ARGUS_RE_SEMVERSemantic version1.2.3 or 1.0.0-beta+build
ARGUS_RE_POS_INTPositive integers123
ARGUS_RE_NEG_INTNegative integers-123
ARGUS_RE_FLOATDecimal numbers3.14 or -2.7
ARGUS_RE_HEXHex numbers0x1A3F
OPTION_STRING('v', "version", HELP("Semantic version"),
VALIDATOR(V_REGEX(ARGUS_RE_SEMVER)))

OPTION_STRING('h', "hex", HELP("Hex number"),
VALIDATOR(V_REGEX(ARGUS_RE_HEX)))

// Custom Patterns

Define your own patterns using MAKE_REGEX:

// Define at file scope
#define RE_PRODUCT_ID MAKE_REGEX("^[A-Z]{2}\\d{4}-[A-Z0-9]{6}$", "Format: XX0000-XXXXXX")
#define RE_LICENSE_KEY MAKE_REGEX("^[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}$", "Format: XXXX-XXXX-XXXX")

ARGUS_OPTIONS(options,
OPTION_STRING('p', "product", HELP("Product ID"),
VALIDATOR(V_REGEX(RE_PRODUCT_ID))),

OPTION_STRING('k', "key", HELP("License key"),
VALIDATOR(V_REGEX(RE_LICENSE_KEY)))
)

// Pattern Testing

Test patterns before use:

# Valid examples
./app --email user@example.com # ✅ Matches ARGUS_RE_EMAIL
./app --ip 192.168.1.1 # ✅ Matches ARGUS_RE_IPV4
./app --date 2024-03-15 # ✅ Matches ARGUS_RE_ISO_DATE

# Invalid examples
./app --email invalid-email # ❌ Error: Invalid value 'invalid-email': Enter email: user@example.com
./app --ip 999.999.999.999 # ❌ Error: Invalid value '999.999.999.999': Enter valid IPv4: 192.168.1.1
./app --date 03-15-2024 # ❌ Error: Invalid value '03-15-2024': Enter date: YYYY-MM-DD

// Error Messages

Each pattern includes helpful error hints:

  • ARGUS_RE_EMAIL: "Enter email: user@example.com"
  • ARGUS_RE_IPV4: "Enter valid IPv4: 192.168.1.1"
  • ARGUS_RE_ISO_DATE: "Enter date: YYYY-MM-DD"

Custom patterns should include similar hints:

#define RE_CUSTOM MAKE_REGEX("pattern", "Clear format description")

// Performance Notes

  • Patterns are compiled once at first use
  • Complex patterns (like IPv6) may impact startup time
  • Use simpler validation when regex isn't needed

// What's Next?