GithubHelp home page GithubHelp logo

sindresorhus / regex Goto Github PK

View Code? Open in Web Editor NEW
329.0 8.0 15.0 162 KB

πŸ”€ Swifty regular expressions

Home Page: https://sindresorhus.com/Regex

License: MIT License

Swift 100.00%
regex regular-expression nsregularexpression swift swifty swift-package

regex's Introduction

Regex

Swifty regular expressions

This is a wrapper for NSRegularExpression that makes it more convenient and type-safe to use regular expressions in Swift.

Install

Add the following to Package.swift:

.package(url: "https://github.com/sindresorhus/Regex", from: "0.1.0")

Or add the package in Xcode.

Usage

First, import the package:

import Regex

Supported regex syntax.

Examples

Check if it matches:

Regex(#"\d+"#).isMatched(by: "123")
//=> true

Get first match:

Regex(#"\d+"#).firstMatch(in: "123-456")?.value
//=> "123"

Get all matches:

Regex(#"\d+"#).allMatches(in: "123-456").map(\.value)
//=> ["123", "456"]

Replacing first match:

"123πŸ¦„456".replacingFirstMatch(of: #"\d+"#, with: "")
//=> "πŸ¦„456"

Replacing all matches:

"123πŸ¦„456".replacingAllMatches(of: #"\d+"#, with: "")
//=> "πŸ¦„"

Named capture groups:

let regex = Regex(#"\d+(?<word>[a-z]+)\d+"#)

regex.firstMatch(in: "123unicorn456")?.group(named: "word")?.value
//=> "unicorn"

Pattern matching:

switch "foo123" {
case Regex(#"^foo\d+$"#):
	print("Match!")
default:
	break
}

switch Regex(#"^foo\d+$"#) {
case "foo123":
	print("Match!")
default:
	break
}

Multiline and comments:

let regex = Regex(
	#"""
	^
	[a-z]+  # Match the word
	\d+     # Match the number
	$
	"""#,
	options: .allowCommentsAndWhitespace
)

regex.isMatched(by: "foo123")
//=> true

API

See the API docs.

FAQ

Why are pattern strings wrapped in #?

Those are raw strings and they make it possible to, for example, use \d without having to escape the backslash.

Related

regex's People

Contributors

sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

regex's Issues

Strongly-typed regex named groups

Just an idea (untested):

struct Result: Codable {
	let word: String?
	let number: Int?
}

let regex = Regex(#"\d+(?<\#(.word)>[a-z]+)(?<\#(.number)>\d+)"#, type: Result.self)

let groups = regex.firstMatch(in: "123unicorn456")?.namedGroups

print(groups.word)
//=> Optional("unicorn")

print(groups.number)
//=> Optional(456)
//             ^ Int

Feedback wanted!

Improve print output

The output when printing Regex, Match, and Group is a bit messy. Would be nice to add some nice representations for them. For example, Regex could have the output /foo/i, where the i comes from the .caseInsensitive option.

Make it Codable

Could be useful to make Regex Codable, but not something I need right now. PR welcome if you need it.

It needs to serialize both the expression and also the regex options.

Add `Regex#namedGroups`?

There's currently a method Regex#group(named:) to get a single named group, but it might be nicer to just add Regex#namedGroups which would be a [String: Group] of group name and group.

Thoughts?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.