Giter Site home page Giter Site logo

ylregex's Introduction

Requirements

  • iOS 10.0+
  • Swift 5.1+

Installation

Cocoapods

To integrate YLRegex into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!

target 'MyApp' do
  # your other pod
  # ...
  pod 'YLRegex'
end

Run pod install to build your dependencies.

Usage

let text = """
<h2>赠少年</h2>
<p>江海相逢客恨多,秋风叶下洞庭波。</p>
<p>酒酣夜别淮阴市,月照高楼一曲歌。</p>
"""

firstMatch

if let result = text.firstMatch(pattern: #"<h(\d+)>(.*?)</h\1>"#) {
    print(result.matchedString) // <h2>赠少年</h2>
    print(result.captures)      // [Optional("2"), Optional("赠少年")]
}

allMatches

let matches = text.allMatches(pattern: "<p>(.*?)</p>")
for match in matches {
    print(match.captures)
}
// [Optional("江海相逢客恨多,秋风叶下洞庭波。")]
// [Optional("酒酣夜别淮阴市,月照高楼一曲歌。")]

match

print("hello, world!".match(pattern: #"\w+, "#))	// true

split

let results = text.split(usingRegex: "<p>(.*?)</p>")
print(results)

split

replacingAll

// 1
let string = "hello, world"
print(string.replacingAll(matching: #"(\w+)(.+?)(\w+)"#, with: "$3$2$1"))
// world, hello

// 2
let result = "<h2>hello</h2>".replacingAll(matching: #"<h(\d+)>(.*?)</h\1>"#) { (result) -> String in
    let level = result.captures[0].flatMap(Int.init)!
    return "<h\(level * 2)>\(result.captures[1]!)</h\(level * 2)>"
}
print(result)  // <h4>hello</h4>

replacingFields

let string = "132 172"
print(string.replacingFields(with: ["1": "<", "2": ">"]))
// <3> <7>

将邮箱保护字段替换为解码后的字段

let text = #"请发送邮件到:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="30515253705152531e535f5d">[email&#160;protected]</a>"#
print(text.decodingProtectedFields())
// 请发送邮件到:[email protected]

License

YLRegex is released under the MIT license. See LICENSE for details.

ylregex's People

Contributors

yuleifuyun avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.