-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Recently, the standard library gained a few AST node methods dealing with regex matching:
- Implement
StringLiteral#scan#15398 is available since Crystal 1.16 - Add yielding variant of
StringLiteral#gsub#16378 is available since Crystal 1.19 - Support
StringLiteral#split(RegexLiteral)#16423 is pending approval
It makes sense that the more fundamental method, #match, should be present as well:
module Crystal::Macros
class StringLiteral
def match(regex : RegexLiteral) : HashLiteral(NumberLiteral | StringLiteral)
end
end
# ditto for SymbolLiteral and MacroId, via `def_string_methods`
class RegexLiteral
def match(regex : StringLiteral | SymbolLiteral | MacroId) : HashLiteral(NumberLiteral | StringLiteral)
end
end
endThe returned match data has the same structure as the individual elements returned by StringLiteral#scan.
There are also cases where the match data is not important, only whether a match is successful. So I propose adding #matches? too:
The existing #=~ behaves like #matches? instead of the actual String#=~, as it returns a BoolLiteral rather than a NumberLiteral of the string index.
Add a 👍 reaction to issues you find important.
ysbaddaden, jwoertink, Sija and jneen