A simple / light weight / independent regular expression extesion for Swift String. 轻量正则表达式

What's New

Fixing absolute offset issue.


String Extension of Regular Expression 简体中文

Get Involed with Perfect!

Star Perfect On Github Stack Overflow Follow Perfect on Twitter Join the Perfect Slack

Swift 4.0 Platforms OS X | Linux License Apache PerfectlySoft Twitter Slack Status

This project provides a light weight / simple regular expression extension for Swift String.

This package builds with Swift Package Manager and is part of the Perfect project. It was written to be stand-alone and so does not require PerfectLib or any other components.

Ensure you have installed and activated the latest Swift 4.0 tool chain.


Add this project as a dependency in your Package.swift file.

.package(url:"", from: "3.1.0")

dependencies: ["RegEx"]

Then please add the following line to the beginning part of swift sources:

import Regex

Quick Start

The following demo shows how to extract substring ranges with a pattern:

var source = "there is a bloody bad bread on my bed."

let ranges = source.match(pattern: "b[a-z]+d")
// it will figure out the range of `blood`, `bad` `bread` and `bed`

// you can do further operations, such as remove:

// the result should be:
// there is a y bad bread on my bed.

API Info

extension String {
	/// test if the string contains certain pattern
 	/// - parameters:
 	///   - pattern: string to recognize
 	/// - return: true for found
  public func contains(pattern: String) -> Bool

  /// find string ranges
  /// - parameters:
  ///   - pattern: string to recognize
  /// - return: a string range array
  public func match(pattern: String) -> [Range<String.Index>]


We are transitioning to using JIRA for all bugs and support related issues, therefore the GitHub issues has been disabled.

If you find a mistake, bug, or any other helpful suggestion you'd like to make on the docs please head over to and raise it.

A comprehensive list of open issues can be found at

Further Information

For more information on the Perfect project, please visit


  • Swift Tools 4.0.0
View More Packages from this Author


  • None
Last updated: Thu Sep 21 2023 23:08:32 GMT-0900 (Hawaii-Aleutian Daylight Time)