# \[Pitch #2\] Regex Literals

**URL:** <https://forums.swift.org/t/pitch-2-regex-literals/56736>\
**Category:** Pitches\
**Created:** [April 13, 2022, 5:56pm UTC](https://forums.swift.org/t/pitch-2-regex-literals/56736 "2022-04-13T17:56:28Z")\
**Posts on this page:** 1\
**Showing post:** 54

<div class="post-metadata">

**Author:** ![YOCKOW](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/yockow/32/20636_2.png) [@YOCKOW](https://forums.swift.org/u/YOCKOW)\
**Post date:** [April 16, 2022, 3:34pm UTC](https://forums.swift.org/t/pitch-2-regex-literals/56736/54 "2022-04-16T15:34:05Z")

</div>

General remarks about bare `/.../`:

I guess the reason why some people support `/.../` is because _"we have seen it in other languages"_.  
However, we have to remember that Swift is different from other languages in many senses.

* * *

First, (as mentioned repeatedly in this thread,) we can define prefix/infix/postfix operators containing `/`.  
Authors simply think it is enough to change the syntax rule of Swift, but the fact that certain number of projects will be broken [has come to light](https://forums.swift.org/t/pitch-2-regex-literals/56736/25).

* * *

Second, regex in Swift may differ from ones in other languages.  
Although this is out of scope of this pitch, it is related still.

> [@Declarative String Processing Overview](https://forums.swift.org/t/declarative-string-processing-overview/52459/95):
>
> When applied with grapheme-cluster semantics (the default if applied to `String` ), it would match grapheme-cluster by grapheme-cluster and comparison obeys canonical equivalence. There are some features that might not be supported, e.g. generalizing some scalar properties to grapheme clusters. Resulting indices would be grapheme-cluster aligned.

Such feature would confuse some folks especially from other languages.  
Let me quote my opinion from pitch#1 thread:

> [@\[Pitch\] Regular Expression Literals](https://forums.swift.org/t/pitch-regular-expression-literals/52820/64):
>
> My thoughts are
> 
> - If Swift adopts similar literals with e.g. Perl, semantics should be similar with Perl.
> - If Swift adopts different semantics from e.g. Perl, literals should be different from Perl.
> 
> That would be called perceived affordance.

* * *

Lastly, Swift has sublime philosophy (I hope).  
I agree that `/.../` is simple and easy to write.  
However, to be simple is not enough to be good in Swift.  
I want to quote Mr. Lattner's utterance:

> [@\`if let\` shorthand](https://forums.swift.org/t/if-let-shorthand/54230/130):
>
> The goal of Swift language design isn't to minimize number of characters in code, it is to find a balance between "expressivity", and "readability". Readability isn't just "what does this line of code do" it is a deeper "what is the lifecycle of maintaining and evolving a codebase, particularly when it is worked on by multiple people, or the project spans years of development".

* * *

`/.../` will certainly break Swift.  
`/.../` is not Swift's.  
Does Swift have to borrow the syntax from others to break itself?  
Will we get more benefit from `/.../` than loss from it?  
_Think different._

---

_[View the full topic](https://forums.swift.org/t/pitch-2-regex-literals/56736)._
