https://github.com/perfectsiderepos/perfect-regex
A simple / light weight / independent regular expression extesion for Swift String. 轻量正则表达式
https://github.com/perfectsiderepos/perfect-regex
regular-expression string swift
Last synced: 5 months ago
JSON representation
A simple / light weight / independent regular expression extesion for Swift String. 轻量正则表达式
- Host: GitHub
- URL: https://github.com/perfectsiderepos/perfect-regex
- Owner: PerfectSideRepos
- License: apache-2.0
- Created: 2017-02-22T15:58:57.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2018-09-07T13:53:27.000Z (over 7 years ago)
- Last Synced: 2025-10-21T09:56:05.404Z (5 months ago)
- Topics: regular-expression, string, swift
- Language: Swift
- Size: 24.4 KB
- Stars: 7
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# String Extension of Regular Expression [简体中文](README.zh_CN.md)
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](https://github.com/PerfectlySoft/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.
## Building
Add this project as a dependency in your Package.swift file.
``` swift
.package(url:"https://github.com/PerfectSideRepos/Perfect-RegEx.git", from: "3.1.0")
...
dependencies: ["Regex"]
```
Then please add the following line to the beginning part of swift sources:
``` swift
import Regex
```
## Quick Start
The following demo shows how to extract substring ranges with a pattern:
``` swift
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`
let found:[String] = ranges.map { String(source[$0]) }
print("found", found)
// you can do further operations, such as remove:
source.removeSubrange(ranges[0])
print(source)
// the result should be:
// there is a y bad bread on my bed.
```
## API Info
``` swift
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]
}
```
## Issues
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 [http://jira.perfect.org:8080/servicedesk/customer/portal/1](http://jira.perfect.org:8080/servicedesk/customer/portal/1) and raise it.
A comprehensive list of open issues can be found at [http://jira.perfect.org:8080/projects/ISS/issues](http://jira.perfect.org:8080/projects/ISS/issues)
## Further Information
For more information on the Perfect project, please visit [perfect.org](http://perfect.org).