Sam Soffes

NSRegularExpression Notes

Posted on

I spent awhile today trying to convert a regular expression from Ruby to NSRegularExpression. It was being dumb and took me awhile to figure it out.

The main this is NSRegularExpression's options. By default Ruby, has AnchorsMatchLines on and NSRegularExpression doesn't. I simply turned that on and had good luck.

Front Matter

Here's my specific case (Jekyll front-matter):

Ruby

/\A(---\s*\n.*?\n?)^(---\s*$\n?)/m

Swift

NSRegularExpression(pattern: "\\A(---\\s*\\n.*?\\n?)^(---\\s*$\\n?)", options: .DotMatchesLineSeparators | .AnchorsMatchLines, error: nil)!