Giter VIP home page Giter VIP logo

regex's Introduction

by Crossroad Labs

Regex

🐧 linux: ready GitHub license Build Status GitHub release Carthage compatible CocoaPods version Platform OS X | iOS | tvOS | watchOS | Linux

Advanced regular expressions for Swift

Goals

Regex library was mainly introduced to fulfill the needs of Swift Express - web application server side framework for Swift.

Still we hope it will be useful for everybody else.

Happy regexing ;)

Features

  • Deep Integration with Swift
    • =~ operator support
    • Swift Pattern Matching (aka switch operator) support
  • Named groups
  • Match checking
  • Extraction/Search functions
  • Replace functions
    • With a pattern
    • With custom replacing function
  • Splitting with a Regular Expression
    • Simple
    • With groups
  • String extensions
  • Supports grapheme clusters 👨‍👩‍👧

Extra

Path to Regex converter is available as a separate library here: PathToRegex

This one allows using path patterns like /folder/*/:file.txt or /route/:one/:two to be converted to Regular Expressions and matched against strings.

Getting started

Installation

Add the following dependency to your Package.swift:

.Package(url: "https://github.com/crossroadlabs/Regex.git", majorVersion: 1)

Run swift build and build your app.

Add the following to your Podfile:

pod 'CrossroadRegex'

Make sure that you are integrating your dependencies using frameworks: add use_frameworks! to your Podfile. Then run pod install.

Add the following to your Cartfile:

github "crossroadlabs/Regex"

Run carthage update and follow the steps as described in Carthage's README.

Manually

  1. Download and drop /Regex folder in your project.
  2. Congratulations!

Examples

Hello Regex:

All the lines below are identical and represent simple matching. All operators and matches function return Bool

//operator way, can match either regex or string containing pattern
"l321321alala" =~ "(.+?)([123]*)(.*)".r
"l321321alala" =~ "(.+?)([123]*)(.*)"

//similar function
"(.+?)([123]*)(.*)".r!.matches("l321321alala")

Operator !~ returns true if expression does NOT match:

"l321321alala" !~ "(.+?)([123]*)(.*)".r
"l321321alala" !~ "(.+?)([123]*)(.*)"
//both return false

Swift Pattern Matching (aka switch keyword)

Regex provides very deep integration with Swift and can be used with the switch keyword in the following way:

let letter = "a"
let digit = "1"
let other = "!"

//you just put your string is a regular Swift's switch to match to regular expressions
switch letter {
	//note .r after the string literal of the pattern
	case "\\d".r: print("digit")
	case "[a-z]".r: print("letter")
	default: print("bizarre symbol")
}

switch digit {
	case "\\d".r: print("digit")
	case "[a-z]".r: print("letter")
	default: print("bizarre symbol")
}

switch other {
	//note .r after the string literal of the pattern
	case "\\d".r: print("digit")
	case "[a-z]".r: print("letter")
	default: print("bizarre symbol")
}

The output of the code above will be:

letter
digit
bizarre symbol

Accessing groups:

// strings can be converted to regex in Scala style .r property of a string
let digits = "(.+?)([123]*)(.*)".r?.findFirst(in: "l321321alala")?.group(at: 2)
// digits is "321321" here

Named groups:

let regex:RegexType = try Regex(pattern:"(.+?)([123]*)(.*)",
                                        groupNames:"letter", "digits", "rest")
let match = regex.findFirst(in: "l321321alala")
if let match = match {
	let letter = match.group(named: "letter")
	let digits = match.group(named: "digits")
	let rest = match.group(named: "rest")
	//do something with extracted data
}

Replace:

let replaced = "(.+?)([123]*)(.*)".r?.replaceAll(in: "l321321alala", with: "$1-$2-$3")
//replaced is "l-321321-alala"

Replace with custom replacer function:

let replaced = "(.+?)([123]+)(.+?)".r?.replaceAll(in: "l321321la321a") { match in
	if match.group(at: 1) == "l" {
		return nil
	} else {
		return match.matched.uppercaseString
	}
}
//replaced is "l321321lA321A"

Split:

In the following example, split() looks for 0 or more spaces followed by a semicolon followed by 0 or more spaces and, when found, removes the spaces from the string. nameList is the array returned as a result of split().

let names = "Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand"
let nameList = names.split(using: "\\s*;\\s*".r)
//name list contains ["Harry Trump", "Fred Barney", "Helen Rigby", "Bill Abel", "Chris Hand"]

Split with groups:

If separator contains capturing parentheses, matched results are returned in the array.

let myString = "Hello 1 word. Sentence number 2."
let splits = myString.split(using: "(\\d)".r)
//splits contains ["Hello ", "1", " word. Sentence number ", "2", "."]

Changelog

You can view the CHANGELOG as a separate document here.

Contributing

To get started, sign the Contributor License Agreement.

regex's People

Contributors

chriszielinski avatar coder-256 avatar dileping avatar finestructure avatar lfarah avatar nayzak avatar ypopovych 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar

regex's Issues

Regular expression not working (all characters are shifted)

Hi there,

I am using a regular expression to look for the url of the 'visually similar' button on google.
This is the regex I use:
"href=((?:(?!href).)*?)>Vis"

and it works perfectly when testing on https://regexr.com/
this is some example to-match text:


enu-panel" role="menu" tabindex="-1" jsaction="keydown:Xiq7wd;mouseover:pKPowd;mouseout:O9bKS" data-ved="2ahUKEwjRitfP9vXvAhXf_7sIHViuBewQqR8wAXoECAMQBQ"><li class="action-menu-item" role="menuitem"><a class="fl" href="https://webcache.googleusercontent.com/search?q=cache:8lDNWm_duSMJ:https://www.trustedshops.com/+&amp;cd=2&amp;hl=en&amp;ct=clnk&amp;gl=de\" ping="/url?sa=t&source=web&rct=j&url=https://webcache.googleusercontent.com/search%3Fq%3Dcache:8lDNWm_duSMJ:https://www.trustedshops.com/%2B%26cd%3D2%26hl%3Den%26ct%3Dclnk%26gl%3Dde&amp;ved=2ahUKEwjRitfP9vXvAhXf_7sIHViuBewQIDABegQIAxAG\">Cached<div class="IsZvec"><span class="aCOpRe">Trusted Shops is the European Trustmark for online shops with money-back guarantee for consumers. Trusted Shops offers a comprehensive service to raise ...<div class="ULSxyf"><div jsmodel="gpo5Gf" class="LnbJhc" data-count="28" style="position:relative" data-iu="1" data-hveid="CAIQAA" data-ved="2ahUKEwjRitfP9vXvAhXf_7sIHViuBewQ8w0oAHoECAIQAA"><div class="e2BEnf U7izfe mfMhoc"><a class="ekf0x hSQtef" href="/search?tbs=simg:CAESiQIJQs8eCt9yzs0a_1QELELCMpwgaOgo4CAQSFNcy_1TP2GfwQ9zXEDcYqnTbjEq4kGhqVVToFJUcTvzott-6Sl5Qp4R6jBL5G5bKsuyAFMAQMCxCOrv4IGgoKCAgBEgTTC8hDDAsQne3BCRqdAQofCgxvZmZpY2UgY2hhaXLapYj2AwsKCS9tLzA4cTF4cAofCgxzd2l2ZWwgY2hhaXLapYj2AwsKCS9tLzBncTZreAoiCg9mdXJuaXR1cmUgc3R5bGXapYj2AwsKCS9qLzl3MHFqcwobCghmb3IgdGVlbtqliPYDCwoJL2EvNnEzMDY3ChgKBXNvbGlk2qWI9gMLCgkvYS8zbWcxY20M&q=trusted+shop&tbm=isch&sa=X&ved=2ahUKEwjRitfP9vXvAhXf_7sIHViuBewQjJkEegQIAhAB"><div class="iv236"><span class="iJddsb" style="height:20px;width:20px"><svg focusable="false" viewbox="0 0 24 24"><path d="M14 13l4 5H6l4-4 1.79 1.78L14 13zm-6.01-2.99A2 2 0 0 0 8 6a2 2 0 0 0-.01 4.01zM22 5v14a3 3 0 0 1-3 2.99H5c-1.64 0-3-1.36-3-3V5c0-1.64 1.36-3 3-3h14c1.65 0 3 1.36 3 3zm-2.01 0a1 1 0 0 0-1-1H5a1 1 0 0 0-1 1v14a1 1 0 0 0 1 1h7v-.01h7a1 1 0 0 0 1-1V5"><div class="iJ1Kvb"><h3 class="GmE3X" aria-level="2" role="heading">Visually similar images<div style="padding-bottom:0" id="iur">

<div jsmodel="" jscontroller="IkchZc" jsaction="PdWSXe:h5M12e;rcuQ6b:npT2md" jsdata="X2sNs;;CiOOHU"><div data-h="130" data-nr="4" style="margin-right:-2px;margin-bottom:-2px"><div jsname="dTDiAc" class="eA0Zlc qN5nNb tapJqb ivg-i" data-docid="DrX4TNBpITAGoM" jsdata="XZxcdf;DrX4TNBpITAGoM;CiOOJI" data-ved="2ahUKEwjRitfP9vXvAhXf_7sIHViuBewQ5r0BegQIIRAA"><a href="/search?q=trusted+shop&tbm=isch&source=iu&ictx=1&tbs=simg:CAESiQIJQs8eCt9yzs0a_1QELELCMpwgaOgo4CAQSFNcy_1TP2GfwQ9zXEDcYqnTbjEq4kGhqVVToFJUcTvzott-6Sl5Qp4R6jBL5G5bKsuyAFMAQMCxCOrv4IGgoKCAgBEgTTC8hDDAsQne3BCRqdAQofCgxvZmZpY2UgY2hhaXLapYj2AwsKCS9tLzA4cTF4cAofCgxzd2l2ZWwgY2hhaXLapYj2AwsKCS9tLzBncTZreAoiCg9mdXJuaXR1cmUgc3R5bGXapYj2AwsKCS9qLzl3MHFqcwobCghmb3IgdGVlbtqliPYDCwoJL2EvNnEzMDY3ChgKBXNvbGlk2qWI9gMLCgkvYS8zbWcxY20M&fir=DrX4TNBpITAGoM%252CO1KPcBx95JlNxM%252C&vet=1&usg=AI4_-


When using this exact same regex with your swift Regex expression I do not get the expected result, but I get the following:


FNcy_1TP2GfwQ9zXEDcYqnTbjEq4kGhqVVToFJUcTvzott-6Sl5Qp4R6jBL5G5bKsuyAFMAQMCxCOrv4IGgoKCAgBEgTTC8hDDAsQne3BCRqdAQofCgxvZmZpY2UgY2hhaXLapYj2AwsKCS9tLzA4cTF4cAofCgxzd2l2ZWwgY2hhaXLapYj2AwsKCS9tLzBncTZreAoiCg9mdXJuaXR1cmUgc3R5bGXapYj2AwsKCS9qLzl3MHFqcwobCghmb3IgdGVlbtqliPYDCwoJL2EvNnEzMDY3ChgKBXNvbGlk2qWI9gMLCgkvYS8zbWcxY20M&q=trusted+shop&tbm=isch&sa=X&ved=2ahUKEwjRitfP9vXvAhXf_7sIHViuBewQjJkEegQIAhAB"><div class="iv236"><span class="iJddsb" style="height:20px;width:20px"><svg focusable="false" viewbox="0 0 24 24"><path d="M14 13l4 5H6l4-4 1.79 1.78L14 13zm-6.01-2.99A2 2 0 0 0 8 6a2 2 0 0 0-.01 4.01zM22 5v14a3 3 0 0 1-3 2.99H5c-1.64 0-3-1.36-3-3V5c0-1.64 1.36-3 3-3h14c1.65 0 3 1.36 3 3zm-2.01 0a1 1 0 0 0-1-1H5a1 1 0 0 0-1 1v14a1 1 0 0 0 1 1h7v-.01h7a1 1 0 0 0 1-1V5">

<div class="iJ1Kvb"><h3 class="GmE3X" aria-level="2" role="heading">Visually similar images<div s


As you can see it somehow captures beyond the last capture group ">Vis". And additionally there are a lot of characters missing from the start of the expected capture.. all the characters next to the "href=".

I tried a lot to rewrite my regex, but as it is confirmed to be working on regex testers I must conclude that there is something wrong with this Regex cocoapod.

Please help! Thank you
A link to the regex helper tool: regexr.com/5qfv4

That's not how regex is

Your README is full of examples of [1,2,3], but character classes do not use commas. So either you really want to match commas, or your README needs to be remedied. 😃

(should be just [123])

Manual Installation

CocoaPods and Carthage are awesome tools and make our life really easier, but there are some devs who still don't know how to use them.

It would be cool to add the Manual installation guide in your README.md. You can take a look at my iOS Readme Template to see how you can do it.

Error in dependencies

When I try importing Regex with the following dependency in my project, I'm getting different contents for the Package.swift file:

//In my own Package.swift file
    ...
    .Package(url: "https://github.com/crossroadlabs/Regex.git", majorVersion: 0),
    ...

Contents of the Regex/Package.swift I'm getting:

import PackageDescription

let package = Package(
    name: "Regex",
    targets: [Target(name: "Regex")],
    dependencies: [.Package(url: "https://github.com/crossroadlabs/Boilerplate.git", majorVersion: 0, minor: 2)]
)

Instead of

import PackageDescription

let package = Package(
    name: "Regex",
    targets: [Target(name: "Regex")],
    dependencies: [.Package(url: "https://github.com/crossroadlabs/Boilerplate.git", Version(1, 0, 0, prereleaseIdentifiers: ["alpha", "1"]))],
    exclude: ["Carthage"]
)

What's odd is that SPM is resolving Regex 0.8.0 but with different contents than this repo.
The issue is that Boilerplate depends on Result and they have both been updated to support Swift 3, but the version of Regex I'm getting is relying on a deprecated version of Boilerplate.

Feature Request: Support in-pattern named groups

I'd love to have this support the same named groups that NSRE supports, like this:

// NSRegularExpression
try! NSRegularExpression(pattern:
        "^(?<major>\\d+)\\." +
        "(?<minor>\\d+)\\." +
        "(?<patch>\\d+)" +
        "(?:-(?<preRelease>(?:(?<preReleaseId>[0-9A-Za-z-]+)\\.?)+))?" +
        "(?:\\+(?<build>(?:(?<buildId>[0-9A-Za-z-]+)\\.?)+))?$",
        options: [])

// Crossroad Labs Regex
try! Regex(pattern:
        "^(?<major>\\d+)\\." +
        "(?<minor>\\d+)\\." +
        "(?<patch>\\d+)" +
        "(?:-(?<preRelease>(?:(?<preReleaseId>[0-9A-Za-z-]+)\\.?)+))?" +
        "(?:\\+(?<build>(?:(?<buildId>[0-9A-Za-z-]+)\\.?)+))?$",
        groupNames: "major", "minor", "patch", "preRelease", "preReleaseId", "build", "buildId")

This feels fragile to me; I feel like it might easily slip so that one group's name doesn't represent the intended group, and that might be subtle enough to not catch in testing.

It's also silly because it accepts the same syntax for in-pattern group names that NSRegularExpression accepts, without using it.

Getting 500x performance drop compared to pure NSRegularExpression

Love the syntax. Love the features. But performance goes bad on large strings. Here's the test for digesting a crashlog.

internal func testRegex() {
    let content: String = try! String(contentsOf: PathUtility.getCrashlogUrl(name: "gifox"))
    let nsContent: NSString = content as NSString

    let pattern: String = "^\\s*0x([A-F0-9]+) - \\s*0x([A-F0-9]+) \\s*\\+?(.+?) .*? \\<([A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12})\\>"

    let regex: Regex = try! Regex(pattern: pattern, options: [RegexOptions.caseInsensitive, RegexOptions.anchorsMatchLines])
    let nsRegex: NSRegularExpression = try! NSRegularExpression(pattern: pattern, options: [NSRegularExpression.Options.caseInsensitive, NSRegularExpression.Options.anchorsMatchLines])

    var date: Date
    var intervalAll: Double
    var match: [String]

    date = Date()
    match = regex.findAll(in: content).map({ $0.group(at: 1)! })
    intervalAll = Date().timeIntervalSince(date)
    Swift.print("regex: ", intervalAll)
    Swift.print(match.count, match)

    date = Date()
    match = nsRegex.matches(in: content, range: NSRange(0 ..< content.characters.count)).map({ nsContent.substring(with: $0.rangeAt(1)) as String })
    intervalAll = Date().timeIntervalSince(date)
    Swift.print("nsRegex: ", intervalAll)
    Swift.print(match.count, match)

    // regex:  7.71533399820328
    // 327 ["101e03000", "102186000", "102348000", "1023bf000", "102451000", "1024de000", "1024fc000", "102573000", "1025c2000", "1027dc000", "102812000", "102847000", "1028bf000", "102914000", "102992000", "1029ee000", "102a3b000", "102a49000", "102a64000", "102f78000", "102f92000", "102fbd000", "102fc8000", "102fda000", "103034000", "103215000", "103220000", "103231000", "103243000", "103250000", "1032ee000", "10335e000", "10336c000", "10337d000", "10343d000", "1034e5000", "10352b000", "103560000", "10359b000", "103613000", "1036c3000", "105225000", "105412000", "1058f3000", "1058f9000", "107afe000", "107ce3000", "108a6e000", "108b4e000", "10913f000", "10982f000", "7fffa6cf2000", "7fffa7038000", "7fffa886e000", "7fffa8a30000", "7fffa8b9d000", "7fffa8bb6000", "7fffa90d0000", "7fffa9242000", "7fffa9257000", "7fffa964e000", "7fffa9665000", "7fffa966c000", "7fffa9681000", "7fffa9809000", "7fffa98bc000", "7fffa98bd000", "7fffa98fb000", "7fffa9b7b000", "7fffaa966000", "7fffaa967000", "7fffaaa6f000", "7fffaab9f000", "7fffaac46000", "7fffaace5000", "7fffaad90000", "7fffaade1000", "7fffaadf1000", "7fffaae3f000", "7fffaae7b000", "7fffaae87000", "7fffab094000", "7fffab1fd000", "7fffab5f1000", "7fffab5f2000", "7fffab5f6000", "7fffab900000", "7fffab904000", "7fffab90a000", "7fffab9a2000", "7fffab9bd000", "7fffab9bf000", "7fffab9c2000", "7fffabaa9000", "7fffabab7000", "7fffabbf3000", "7fffabc81000", "7fffabc95000", "7fffabf91000", "7fffac03e000", "7fffac4d8000", "7fffacb5b000", "7fffacd9f000", "7ffface05000", "7fffaceb7000", "7fffacf03000", "7fffacf04000", "7fffacf56000", "7fffad232000", "7fffad266000", "7fffad26f000", "7fffad3dc000", "7fffad48d000", "7fffad4ed000", "7fffad55e000", "7fffad62c000", "7fffad779000", "7fffad7af000", "7fffad853000", "7fffad91e000", "7fffadab5000", "7fffade87000", "7fffadf78000", "7fffae01c000", "7fffae0b2000", "7fffae0b9000", "7fffae10b000", "7fffae26b000", "7fffae270000", "7fffae361000", "7fffae385000", "7fffae3ad000", "7fffae3b0000", "7fffae55c000", "7fffaf165000", "7fffaf17f000", "7fffaf213000", "7fffaf407000", "7fffaf423000", "7fffaf95d000", "7fffb02a1000", "7fffb0480000", "7fffb0489000", "7fffb04d8000", "7fffb04f2000", "7fffb04fe000", "7fffb0501000", "7fffb0505000", "7fffb050a000", "7fffb0514000", "7fffb0520000", "7fffb055d000", "7fffb06d4000", "7fffb107d000", "7fffb1128000", "7fffb1270000", "7fffb14db000", "7fffb15a3000", "7fffb1aca000", "7fffb1aee000", "7fffb1bdc000", "7fffb1bdd000", "7fffb1dde000", "7fffb1e34000", "7fffb2344000", "7fffb2647000", "7fffb26bd000", "7fffb26e7000", "7fffb2a71000", "7fffb2ae1000", "7fffb2e90000", "7fffb4217000", "7fffb4404000", "7fffb5333000", "7fffb534f000", "7fffb535d000", "7fffb5380000", "7fffb547a000", "7fffb5506000", "7fffb5519000", "7fffb5535000", "7fffb553e000", "7fffb5542000", "7fffb5938000", "7fffb6052000", "7fffb6641000", "7fffb6f8d000", "7fffb705d000", "7fffb70ae000", "7fffb711a000", "7fffb74fa000", "7fffb75c5000", "7fffb76de000", "7fffb77b3000", "7fffb77d0000", "7fffb7a29000", "7fffb7cea000", "7fffb7d7a000", "7fffb7eba000", "7fffb7fda000", "7fffb8040000", "7fffb80c0000", "7fffb80cb000", "7fffb8105000", "7fffb81b6000", "7fffb81f6000", "7fffb8449000", "7fffb85ed000", "7fffb9d75000", "7fffb9d82000", "7fffba046000", "7fffba07b000", "7fffba773000", "7fffbadf9000", "7fffbae04000", "7fffbae0c000", "7fffbae0d000", "7fffbae22000", "7fffbaee0000", "7fffbaf2b000", "7fffbb752000", "7fffbba03000", "7fffbbbd8000", "7fffbbbf6000", "7fffbbc1f000", "7fffbbcd0000", "7fffbc128000", "7fffbc5b3000", "7fffbc637000", "7fffbccdc000", "7fffbccf7000", "7fffbcd0f000", "7fffbd9f8000", "7fffbda8a000", "7fffbdb38000", "7fffbdf7c000", "7fffbe672000", "7fffbeb24000", "7fffbebb6000", "7fffbecc5000", "7fffbecdb000", "7fffbecf0000", "7fffbecf2000", "7fffbeefc000", "7fffbf951000", "7fffbfe5c000", "7fffbff33000", "7fffbff61000", "7fffbff8a000", "7fffbffa6000", "7fffc0043000", "7fffc0045000", "7fffc0274000", "7fffc027c000", "7fffc027d000", "7fffc0282000", "7fffc02ef000", "7fffc031b000", "7fffc039b000", "7fffc039c000", "7fffc03ad000", "7fffc03bc000", "7fffc0413000", "7fffc043e000", "7fffc044f000", "7fffc0466000", "7fffc0467000", "7fffc0522000", "7fffc07a5000", "7fffc084a000", "7fffc0873000", "7fffc0883000", "7fffc0889000", "7fffc097c000", "7fffc0ba7000", "7fffc0ba9000", "7fffc0bc3000", "7fffc0bda000", "7fffc0eb5000", "7fffc0f30000", "7fffc1305000", "7fffc130a000", "7fffc1358000", "7fffc1375000", "7fffc13af000", "7fffc13c2000", "7fffc13c4000", "7fffc156d000", "7fffc1606000", "7fffc1614000", "7fffc1704000", "7fffc172e000", "7fffc174e000", "7fffc1753000", "7fffc175e000", "7fffc1766000", "7fffc176f000", "7fffc17f3000", "7fffc1825000", "7fffc182b000", "7fffc182c000", "7fffc1839000", "7fffc183a000", "7fffc1840000", "7fffc1843000", "7fffc1845000", "7fffc185e000", "7fffc185f000", "7fffc18ed000", "7fffc18f1000", "7fffc18f5000", "7fffc190e000", "7fffc1915000", "7fffc193f000", "7fffc1962000", "7fffc19aa000", "7fffc19c9000", "7fffc1a23000", "7fffc1a2d000", "7fffc1a37000", "7fffc1a40000", "7fffc1a4b000", "7fffc1a4f000", "7fffc1a51000", "7fffc1a59000", "7fffc1a6d000", "7fffc1a73000"]
    // nsRegex:  0.0154680013656616
    // 327 ["101e03000", "102186000", "102348000", "1023bf000", "102451000", "1024de000", "1024fc000", "102573000", "1025c2000", "1027dc000", "102812000", "102847000", "1028bf000", "102914000", "102992000", "1029ee000", "102a3b000", "102a49000", "102a64000", "102f78000", "102f92000", "102fbd000", "102fc8000", "102fda000", "103034000", "103215000", "103220000", "103231000", "103243000", "103250000", "1032ee000", "10335e000", "10336c000", "10337d000", "10343d000", "1034e5000", "10352b000", "103560000", "10359b000", "103613000", "1036c3000", "105225000", "105412000", "1058f3000", "1058f9000", "107afe000", "107ce3000", "108a6e000", "108b4e000", "10913f000", "10982f000", "7fffa6cf2000", "7fffa7038000", "7fffa886e000", "7fffa8a30000", "7fffa8b9d000", "7fffa8bb6000", "7fffa90d0000", "7fffa9242000", "7fffa9257000", "7fffa964e000", "7fffa9665000", "7fffa966c000", "7fffa9681000", "7fffa9809000", "7fffa98bc000", "7fffa98bd000", "7fffa98fb000", "7fffa9b7b000", "7fffaa966000", "7fffaa967000", "7fffaaa6f000", "7fffaab9f000", "7fffaac46000", "7fffaace5000", "7fffaad90000", "7fffaade1000", "7fffaadf1000", "7fffaae3f000", "7fffaae7b000", "7fffaae87000", "7fffab094000", "7fffab1fd000", "7fffab5f1000", "7fffab5f2000", "7fffab5f6000", "7fffab900000", "7fffab904000", "7fffab90a000", "7fffab9a2000", "7fffab9bd000", "7fffab9bf000", "7fffab9c2000", "7fffabaa9000", "7fffabab7000", "7fffabbf3000", "7fffabc81000", "7fffabc95000", "7fffabf91000", "7fffac03e000", "7fffac4d8000", "7fffacb5b000", "7fffacd9f000", "7ffface05000", "7fffaceb7000", "7fffacf03000", "7fffacf04000", "7fffacf56000", "7fffad232000", "7fffad266000", "7fffad26f000", "7fffad3dc000", "7fffad48d000", "7fffad4ed000", "7fffad55e000", "7fffad62c000", "7fffad779000", "7fffad7af000", "7fffad853000", "7fffad91e000", "7fffadab5000", "7fffade87000", "7fffadf78000", "7fffae01c000", "7fffae0b2000", "7fffae0b9000", "7fffae10b000", "7fffae26b000", "7fffae270000", "7fffae361000", "7fffae385000", "7fffae3ad000", "7fffae3b0000", "7fffae55c000", "7fffaf165000", "7fffaf17f000", "7fffaf213000", "7fffaf407000", "7fffaf423000", "7fffaf95d000", "7fffb02a1000", "7fffb0480000", "7fffb0489000", "7fffb04d8000", "7fffb04f2000", "7fffb04fe000", "7fffb0501000", "7fffb0505000", "7fffb050a000", "7fffb0514000", "7fffb0520000", "7fffb055d000", "7fffb06d4000", "7fffb107d000", "7fffb1128000", "7fffb1270000", "7fffb14db000", "7fffb15a3000", "7fffb1aca000", "7fffb1aee000", "7fffb1bdc000", "7fffb1bdd000", "7fffb1dde000", "7fffb1e34000", "7fffb2344000", "7fffb2647000", "7fffb26bd000", "7fffb26e7000", "7fffb2a71000", "7fffb2ae1000", "7fffb2e90000", "7fffb4217000", "7fffb4404000", "7fffb5333000", "7fffb534f000", "7fffb535d000", "7fffb5380000", "7fffb547a000", "7fffb5506000", "7fffb5519000", "7fffb5535000", "7fffb553e000", "7fffb5542000", "7fffb5938000", "7fffb6052000", "7fffb6641000", "7fffb6f8d000", "7fffb705d000", "7fffb70ae000", "7fffb711a000", "7fffb74fa000", "7fffb75c5000", "7fffb76de000", "7fffb77b3000", "7fffb77d0000", "7fffb7a29000", "7fffb7cea000", "7fffb7d7a000", "7fffb7eba000", "7fffb7fda000", "7fffb8040000", "7fffb80c0000", "7fffb80cb000", "7fffb8105000", "7fffb81b6000", "7fffb81f6000", "7fffb8449000", "7fffb85ed000", "7fffb9d75000", "7fffb9d82000", "7fffba046000", "7fffba07b000", "7fffba773000", "7fffbadf9000", "7fffbae04000", "7fffbae0c000", "7fffbae0d000", "7fffbae22000", "7fffbaee0000", "7fffbaf2b000", "7fffbb752000", "7fffbba03000", "7fffbbbd8000", "7fffbbbf6000", "7fffbbc1f000", "7fffbbcd0000", "7fffbc128000", "7fffbc5b3000", "7fffbc637000", "7fffbccdc000", "7fffbccf7000", "7fffbcd0f000", "7fffbd9f8000", "7fffbda8a000", "7fffbdb38000", "7fffbdf7c000", "7fffbe672000", "7fffbeb24000", "7fffbebb6000", "7fffbecc5000", "7fffbecdb000", "7fffbecf0000", "7fffbecf2000", "7fffbeefc000", "7fffbf951000", "7fffbfe5c000", "7fffbff33000", "7fffbff61000", "7fffbff8a000", "7fffbffa6000", "7fffc0043000", "7fffc0045000", "7fffc0274000", "7fffc027c000", "7fffc027d000", "7fffc0282000", "7fffc02ef000", "7fffc031b000", "7fffc039b000", "7fffc039c000", "7fffc03ad000", "7fffc03bc000", "7fffc0413000", "7fffc043e000", "7fffc044f000", "7fffc0466000", "7fffc0467000", "7fffc0522000", "7fffc07a5000", "7fffc084a000", "7fffc0873000", "7fffc0883000", "7fffc0889000", "7fffc097c000", "7fffc0ba7000", "7fffc0ba9000", "7fffc0bc3000", "7fffc0bda000", "7fffc0eb5000", "7fffc0f30000", "7fffc1305000", "7fffc130a000", "7fffc1358000", "7fffc1375000", "7fffc13af000", "7fffc13c2000", "7fffc13c4000", "7fffc156d000", "7fffc1606000", "7fffc1614000", "7fffc1704000", "7fffc172e000", "7fffc174e000", "7fffc1753000", "7fffc175e000", "7fffc1766000", "7fffc176f000", "7fffc17f3000", "7fffc1825000", "7fffc182b000", "7fffc182c000", "7fffc1839000", "7fffc183a000", "7fffc1840000", "7fffc1843000", "7fffc1845000", "7fffc185e000", "7fffc185f000", "7fffc18ed000", "7fffc18f1000", "7fffc18f5000", "7fffc190e000", "7fffc1915000", "7fffc193f000", "7fffc1962000", "7fffc19aa000", "7fffc19c9000", "7fffc1a23000", "7fffc1a2d000", "7fffc1a37000", "7fffc1a40000", "7fffc1a4b000", "7fffc1a4f000", "7fffc1a51000", "7fffc1a59000", "7fffc1a6d000", "7fffc1a73000"]
}

=~ and !~ don't work

I installed library with the latest cocoapods

target 'CardSampleTestTask' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for CardSampleTestTask
  pod 'CrossroadRegex'

  target 'CardSampleTestTaskTests' do
    inherit! :search_paths
    # Pods for testing
  end

  target 'CardSampleTestTaskUITests' do
    # Pods for testing
  end

end

Opened the project as a workspace and I have an error Cannot find operator '!~' in scope

Swift 4 - 'range(at:)' has been renamed to 'rangeAt(_:)'

I'm using your lib with the version 1.1.0

I got this error message for the file Match.swift.

The following are the affected lines:
117: let stringRange = try? match.range(at: i).asRange(ofString: source)
133: return try? match.range(at: index).asRange(ofString: source)
145: return try? match.range(at: groupIndex).asRange(ofString: source)

Can not build swift package

Can not build lib using swift package manager.

Swift version: 3.1
Log:

error: invalid target name at 'Tests/Regex'; name of test targets must end in 'Tests'
error: invalid target name at 'Tests/Boilerplate'; name of test targets must end in 'Tests'
error: invalid target name at 'Tests/Result'; name of test targets must end in 'Tests'

Error on swift build

Hello;
I've got this error on both Mac OS X and Linux when trying to use crossroadlabs/Regex:

error: the module at Tests/Result has an invalid name ('Result'): the name of a test module has no ‘Tests’ suffix
fix: rename the module at ‘Tests/Result’ to have a ‘Tests’ suffix

Version Bump

Looks like you forgot to run pod trunk push on the version bump, it's not showing up:

$ pod trunk info CrossroadRegex

CrossroadRegex
    - Versions:
      - 0.3 (2016-01-24 23:51:25 UTC)
      - 0.4 (2016-01-25 16:37:05 UTC)
      - 0.4.1 (2016-02-09 17:47:28 UTC)
      - 0.5 (2016-02-18 10:30:15 UTC)
      - 0.5.2 (2016-02-18 11:32:27 UTC)
      - 0.6.0 (2016-04-06 11:29:12 UTC)
      - 0.7.0 (2016-06-18 19:31:02 UTC)
      - 0.8.0 (2016-07-28 18:40:03 UTC)
      - 1.0.0 (2017-04-15 13:47:51 UTC)
      - 1.0.0-alpha.1 (2016-11-04 18:51:05 UTC)
    - Owners:
      - Daniel Leping <[email protected]>

RegEx not matched due to extended Unicode characters

When a string contains extended Unicode characters (like emojis), RegEx are not applied to its entirety.
This is usually unnoticed but when you try to match something at the very end of a string containing emojis, you can spend hours trying to find an issue in your RegEx…

This is probably linked to the NSString/String mapping used for Swift RegEx functions, explained here:
https://stackoverflow.com/questions/29756530/swift-regex-matching-fails-when-source-contains-unicode-characters

Replacing the few occurrences of

    let range = GroupRange(location: 0, length: source.characters.count)

by

    let range = GroupRange(location: 0, length: (source as NSString).length)

seems to make everything working fine.

Fails to build under Carthage and Xcode 8.1

My Cartfile:

github "crossroadlabs/Regex" == 0.8.0

Build results:

$ carthage update
*** Fetching Regex
*** Fetching Boilerplate
*** Fetching Result
*** Checking out Result at "2.0.1"
*** Checking out Boilerplate at "0.2.4"
*** Checking out Regex at "0.8.0"
*** xcodebuild output can be found in /var/folders/16/jfb809_s2fz01ql7k494f6pc0000gn/T/carthage-xcodebuild.lQCr2C.log
*** Building scheme "Result-watchOS" in Result.xcodeproj
2016-11-20 21:11:41.507 xcodebuild[17389:36782835] [MT] PluginLoading: Required plug-in compatibility UUID DA4FDFD8-C509-4D8B-8B55-84A7B66AE701 for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/SparkInspectorXcodePlugin.xcplugin' not present in DVTPlugInCompatibilityUUIDs
2016-11-20 21:11:41.507 xcodebuild[17389:36782835] [MT] PluginLoading: Required plug-in compatibility UUID DA4FDFD8-C509-4D8B-8B55-84A7B66AE701 for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/ClangFormat.xcplugin' not present in DVTPlugInCompatibilityUUIDs
2016-11-20 21:11:41.970 xcodebuild[17389:36782835] [MT] IDEProvisioning: Migrating '<Xcode3Target:0x7fb539196710:Result-watchOS>' to use provisioning style
2016-11-20 21:11:41.970 xcodebuild[17389:36782835] [MT] IDEProvisioning: Configuration 'Debug' for target '<Xcode3Target:0x7fb539196710:Result-watchOS>' is automatic
2016-11-20 21:11:41.970 xcodebuild[17389:36782835] [MT] IDEProvisioning: Configuration 'Release' for target '<Xcode3Target:0x7fb539196710:Result-watchOS>' is automatic
** CLEAN FAILED **


The following build commands failed:
	Check dependencies
(1 failure)
** BUILD FAILED **


The following build commands failed:
	Check dependencies
(1 failure)
A shell task (/usr/bin/xcrun xcodebuild -project /Users/MyProj/Carthage/Checkouts/Result/Result.xcodeproj -scheme Result-watchOS -configuration Release -sdk watchos ONLY_ACTIVE_ARCH=NO BITCODE_GENERATION_MODE=bitcode CODE_SIGNING_REQUIRED=NO CODE_SIGN_IDENTITY= CARTHAGE=YES clean build) failed with exit code 65:
2016-11-20 21:11:41.507 xcodebuild[17389:36782835] [MT] PluginLoading: Required plug-in compatibility UUID DA4FDFD8-C509-4D8B-8B55-84A7B66AE701 for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/SparkInspectorXcodePlugin.xcplugin' not present in DVTPlugInCompatibilityUUIDs
2016-11-20 21:11:41.507 xcodebuild[17389:36782835] [MT] PluginLoading: Required plug-in compatibility UUID DA4FDFD8-C509-4D8B-8B55-84A7B66AE701 for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/ClangFormat.xcplugin' not present in DVTPlugInCompatibilityUUIDs
2016-11-20 21:11:41.970 xcodebuild[17389:36782835] [MT] IDEProvisioning: Migrating '<Xcode3Target:0x7fb539196710:Result-watchOS>' to use provisioning style
2016-11-20 21:11:41.970 xcodebuild[17389:36782835] [MT] IDEProvisioning: Configuration 'Debug' for target '<Xcode3Target:0x7fb539196710:Result-watchOS>' is automatic
2016-11-20 21:11:41.970 xcodebuild[17389:36782835] [MT] IDEProvisioning: Configuration 'Release' for target '<Xcode3Target:0x7fb539196710:Result-watchOS>' is automatic
** CLEAN FAILED **


The following build commands failed:
	Check dependencies
(1 failure)
** BUILD FAILED **


The following build commands failed:
	Check dependencies
(1 failure)

Strings containing emojis produce shifted results

I seems that when emojis are present it shifts the resulting groups and matched strings.

For example I was using

let textRegex = "\\[([^]]*)\\]".r!

let input = """
Mexican culture has lots of rich history and great food! 🌯🌯🌯🌯

Avacado's are already incredibly popular and for good reason: They taste good, work in tons or recipes, and are [good for you]<url>{https://www.healthline.com/nutrition/12-proven-benefits-of-avocado}. So maybe you already have [avocado]<trend>{avocado-intake} every day or maybe just every once in a while, but maybe there's even more reasons to love these green fatty fruits! [Avacado's have been shown to improve sleep]<url>{https://www.cbsnews.com/pictures/foods-that-will-help-you-sleep-better/9/}.
"""

for text in textRegex.findAll(in: input).makeIterator(){
    print(text.matched)
}

This produces:
d for you]<url
cado]<tre
cado's have been shown to improve sleep]<url

Instead of the expected:
good for you
avocado
Avacado's have been shown to improve sleep

The shifting is caused by the presence of the emoji. Each emoji shifts the results index by 1, so here its shifted by 4.

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.