Search code examples
regexlanguage-design

Why can't Regular Expressions use keywords instead of characters?


Okay, I barely understand RegEx basics, but why couldn't they design it to use keywords (like SQL) instead of some cryptic wildcard characters and symbols?

Is it for performance since the RegEx is interpreted/parsed at runtime? (not compiled)

Or maybe for speed of writing? Considering that when you learn some "simple" character combinations it becomes easier to type 1 character instead of a keyword?


Solution

  • You really want this?

    Pattern findGamesPattern = Pattern.With.Literal(@"<div")
        .WhiteSpace.Repeat.ZeroOrMore
        .Literal(@"class=""game""").WhiteSpace.Repeat.ZeroOrMore.Literal(@"id=""")
        .NamedGroup("gameId", Pattern.With.Digit.Repeat.OneOrMore)
        .Literal(@"-game""")
        .NamedGroup("content", Pattern.With.Anything.Repeat.Lazy.ZeroOrMore)
        .Literal(@"<!--gameStatus")
        .WhiteSpace.Repeat.ZeroOrMore.Literal("=").WhiteSpace.Repeat.ZeroOrMore
        .NamedGroup("gameState", Pattern.With.Digit.Repeat.OneOrMore)
        .Literal("-->");
    

    Ok, but it's your funeral, man.

    Download the library that does this here:
    http://flimflan.com/blog/ReadableRegularExpressions.aspx