[Pitch] Regex-powered string processing algorithms

I agree in general. I think that first-match semantics can be very useful and we should have a nice way to explicitly request that. But, first-match being the default lands the programmer in a perilous situation when the input is dynamic and unpredictable: new inputs might just happen to trigger an earlier case statement in non-obvious ways. One of the main advantages of Swift over C++ at the memory safety level, and over scripting languages at the type safety with explicit error handling level, is that it reduces the degree of programmer vigilance required to write and understand code. This of course always needs to be balanced with brevity and ergonomics. I think whole-match by default reduces programmer vigilance here.

I think an interesting direction to explore are regex modifiers for this purpose. If the case isn't a regex literal, it's a harder and more obnoxious to try to stick anchors in, or to try to insert a .* at beginning or end.

@nnnnnnnn, I believe [Pitch] Unicode for String Processing has most of the modifiers / options. What do you think about modifiers to change how a regex matches? E.g.

switch myString {
  case aRegex: ...
  case anotherRegex.matchingAnywhere: ...
  default: ...
}
4 Likes