-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new String extension for simple regex support
- Loading branch information
1 parent
6b1f513
commit 61ef7f2
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//********************************************************************************************************************** | ||
// | ||
// String+Regex.swift | ||
// Simple regex functions | ||
// Copyright ©2018 Peter Baumgartner. All rights reserved. | ||
// | ||
//********************************************************************************************************************** | ||
|
||
|
||
import Foundation | ||
|
||
|
||
//---------------------------------------------------------------------------------------------------------------------- | ||
|
||
|
||
// MARK: - | ||
|
||
public extension String | ||
{ | ||
|
||
/// Returns any matches for the supplied regex pattern | ||
/// - parameter pattern: A valid regex pattern | ||
/// - parameter options: The options inluence the behavior of the NSRegularExpression | ||
/// - returns: An array of strings matching the regex pattern | ||
|
||
public func regexMatches(for pattern: String, options: NSRegularExpression.Options = []) -> [String] | ||
{ | ||
if let regex = try? NSRegularExpression(pattern:pattern, options:options) | ||
{ | ||
let string = self as NSString | ||
let range = NSRange(location:0, length:string.length) | ||
|
||
return regex.matches(in:self, options:[], range:range).map | ||
{ | ||
string.substring(with: $0.range) | ||
} | ||
} | ||
|
||
return [] | ||
} | ||
|
||
|
||
/// Returns true if a string has at least one match for the supplied regex pattern | ||
|
||
public static func ~= (_ string: String, _ pattern: String) -> Bool | ||
{ | ||
return !string.regexMatches(for:pattern).isEmpty | ||
} | ||
|
||
} | ||
|
||
|
||
//---------------------------------------------------------------------------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// String+UniqueByIncrementingTests.swift | ||
// BXSwiftUtils | ||
// | ||
// Created by Peter Baumgartner on 12.10.18. | ||
// Copyright © 2018 Boinx Software Ltd. & Imagine GbR. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
import BXSwiftUtils | ||
|
||
class String_RegexTests: XCTestCase | ||
{ | ||
|
||
func testPositive() | ||
{ | ||
let str = "Bla Laber ABC Schwafel Sülz" | ||
let matches = str.regexMatches(for:"ABC") | ||
XCTAssertEqual(matches,["ABC"]) | ||
} | ||
|
||
func testNegative() | ||
{ | ||
let str = "Bla Laber ABC Schwafel Sülz" | ||
let matches = str.regexMatches(for:"XYZ") | ||
XCTAssertEqual(matches,[]) | ||
} | ||
|
||
func testMatchCount() | ||
{ | ||
let str = "Bla ABC Laber ABC Schwafel ABC Sülz" | ||
let matches = str.regexMatches(for:"ABC") | ||
XCTAssertEqual(matches.count,3) | ||
} | ||
|
||
func testOperator() | ||
{ | ||
let str = "Bla Laber ABC Schwafel Sülz" | ||
let matches = str ~= "ABC" | ||
XCTAssertEqual(matches,true) | ||
} | ||
} |