Swiftpack.co - Package - vadymmarkov/Rexy

Rexy

CI Status Mac OS X Linux Swift License

Rexy is a pure Swift implementation of POSIX Regular Expressions.

Features

  • [x] Pattern matching
  • [x] Capturing groups
  • [x] Replace method
  • [x] Matching operators
  • [x] Representation of a regular expression error
  • [x] Option sets with default constants for compilation flags (cflag) and regex matching flags (eflag)
  • [x] Unit test coverage
  • [x] No dependencies

Usage

Pattern matching

When you want to check if a given string matches regular expression:

import Rexy

// Regular way
do {
  let regex = try Regex("Tyrannosaurus")
  regex.isMatch("Tyrannosaurus") // => true
  regex.isMatch("Spinosaurus") // => false
} catch {
  print(error)
}

// With custom operators
"Tyrannosaurus" =~ "T.*" // true
"Spinosaurus" =~ "T.*" // false
"Spinosaurus" !~ "T.*" // true

Matches

When you want to search an input string for all occurrences of a regular expression and get the matches:

import Rexy

do {
  let regex = try Regex("[a-z]+")
  regex.matches("a1b1") // ["a", "b"])
} catch {
  print(error)
}

When you're interested only in the first occurrence:

import Rexy

do {
  let regex = try Regex("[a-z]+")
  regex.matches("a1b1") // "a"
} catch {
  print(error)
}

Capturing Groups

When you want to match and capture groups:

import Rexy

do {
  let regex = try Regex("(Tyrannosaurus) (Rex)")
  regex.groups("Tyrannosaurus Rex") // => ["Tyrannosaurus", "Rex"]
  regex.groups("Spinosaurus") // => []
} catch {
  print(error)
}

Replace

When you want to replace all strings that match a regular expression pattern with a specified replacement string:

import Rexy

do {
  let regex = try! Regex("Tyrannosaurus")
  regex.replace("Tyrannosaurus Rex Tyrannosaurus", with: "Dinosaur") // => "Dinosaur Rex Dinosaur"
  regex.replace("Spinosaurus", with: "Dinosaur") // => Spinosaurus
} catch {
  print(error)
}

Installation

Rexy is available through Swift Package Manager. To install it, simply add the following lines to your Package.swift:

.Package(url: "https://github.com/vadymmarkov/Rexy.git", versions: Version(0,1,0)..<Version(1,0,0))

Author

Vadym Markov, markov.vadym@gmail.com

Credits

Credits for inspiration go to POSIXRegex by Zewo

Contributing

Check the CONTRIBUTING file for more info.

License

Rexy is available under the MIT license. See the LICENSE file for more info.

Github

link
Stars: 13
Help us keep the lights on

Dependencies

Releases

0.7.0 - Apr 10, 2017

Fix build with latest swift 3.0.2 be @sportlabsMike https://github.com/vadymmarkov/Rexy/pull/11

0.6.1 - Jun 30, 2016

0.6.0 - Jun 30, 2016

Introduce 3 matching methods:

  • isMatch
  • match
  • matches

0.5.0 - Jun 15, 2016

Introduce custom matching operators: https://github.com/vadymmarkov/Rexy/pull/6

0.4.0 - Jun 15, 2016

  • Pattern matching
  • Capturing groups
  • Replace method
  • Representation of a regular expression error
  • Option sets with default constants for compilation flags (cflag) and regex matching flags (eflag)