Skip to content

Regular Expressions #244

@Flipez

Description

@Flipez

Proposed API:

// Regex literals
pattern = /\d{3}-\d{4}/

// Match operations
phone = "555-1234"
phone.match(/\d{3}-\d{4}/)  // Returns match object or nil
phone.match?(/\d{3}/)       // Returns boolean

// Replace with patterns
text = "Hello 123 World 456"
text.gsub(/\d+/, "X")       // "Hello X World X"
text.sub(/\d+/, "X")        // "Hello X World 456" (first only)

// Split with patterns
"a1b2c3".split(/\d/)        // ["a", "b", "c"]

// Scan for all matches
"abc 123 def 456".scan(/\d+/)  // ["123", "456"]

// Capture groups
match = "2024-01-17".match(/(\d{4})-(\d{2})-(\d{2})/)
match[1]  // "2024"
match[2]  // "01"
match[3]  // "17"

// Match operator
if phone =~ /\d{3}-\d{4}/
  puts("Valid phone")
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions