当前位置:网站首页>regular expression
regular expression
2022-07-29 01:09:00 【Hanyang Li】
1. Introduce
Is an independent language , Applicable to all development languages , Even the editor
1. Match scheme
. Match any character , Except for line breaks
* Match any number of characters
? Match as little as possible
2. Common functions
firstMatch In the specified string , lookup ‘ first ’ and pattern Match string
matches In the specified string , Find all and pattern Matching string
3. Common methods
range(index)
index = 0 Get all and pattern Matching is always a string
index = 1 Fetch pattern in ( first ) Internal content , In turn, increasing
4.pattern How to write
1> Copy the complete string directly to pattern
2> Use the content you need to get (.*?) Set up
3> Will not care about the content , Variable content usage “.*?” Filter and ignore , It can adapt to more data matching
5. Because regular expressions are applicable to all languages , Common regular expressions , You can search for
2. Create classification String+Regex.swift
import Foundation
extension String{
/// From the current string , Filter links and text
/// Tuples , You can allow a function to return multiple values
func href() -> (link: String, text: String)?{
// Create regular expression
// Match scheme - Specially used to filter strings
// throws in the light of patern Whether the exception handling is correct
let pattern = "<a href=\"(.*?)\" .*?>(.*?)</a>"
//throws in the light of pattern Whether the exception handling is correct
let regex = try! NSRegularExpression(pattern: pattern, options: [])
guard let result = regex.firstMatch(in: self, options: [], range: NSRange(location: 0, length: self.count)) else{
print(" No matching items ")
return nil
}
let str = (self as NSString)
let r1 = result.range(at: 1)
let link = str.substring(with: r1)
let r2 = result.range(at: 2)
let text = str.substring(with: r2)
//print(link + "\n" + text)
return (link,text)
}
}3. The test method
let str = "aaaa zxzc <a href=\"http://app.baidu.com/t/feed/6vtZb0\" rel=\"nofollow\"> Baidu baidu.com</a> asda"
print(str.href()?.link)
print(str.href()?.text)4. Output results
Optional("http://app.baidu.com/t/feed/6vtZb0")
Optional(" Baidu baidu.com")
边栏推荐
- [target detection] Introduction to yolor theory + practical test visdrone data set
- Day2:三种语言暴刷牛客130题
- What opportunities does the London gold real-time market bring?
- Cookie和Session
- iNFTnews | 元宇宙购物体验将成为吸引消费者的一大利器
- Wechat campus bathroom reservation for the finished product of applet graduation design (7) mid term inspection report
- 转:认知亚文化
- Recursion and divide and conquer
- Deep learning | matlab implementation of TCN time convolution neural network spatialdropoutlayer parameter description
- Charles -- teach you how to use the packet capturing tool from 0-1
猜你喜欢

对接支付宝支付

The method of tracking the real-time market of London Silver

🧐 table1 | 一秒搞定你的三线表

How to explain JS' bind simulation implementation to your girlfriend

Deep learning | matlab implementation of TCN time convolution neural network spatialdropoutlayer parameter description

Summary of process and thread knowledge points 1

【unity】将unity编辑c#配置为vscode

Some considerations about ThreadPool

Thread lock and its ascending and descending levels

SystemVerilog-连接和复制运算符
随机推荐
Summary of process and thread knowledge points 2
Y80. Chapter 4 Prometheus big factory monitoring system and practice -- Kube state metrics component introduction and monitoring extension (XI)
【commons-lang3专题】004- NumberUtils 专题
The method of tracking the real-time market of London Silver
A new generation of ultra safe cellular battery, Sihao aipao, is on the market, starting from 139900
Day2:三种语言暴刷牛客130题
ActiveMQ basic details
[web development] basic knowledge of flask framework
MySQL stored procedure realizes the creation of a table (copy the structure of the original table and create a new table)
AQS principle
[Commons lang3 topic] 005- objectutils topic
如何处理项目中的时间、范围和成本限制?
【commons-lang3专题】001-StringUtils 专题
量化交易之数字货币篇 - 生成foot print因子数据
In the second round, 1000 okaleido tiger were sold out in one hour after logging in to binance NFT again
Day2: 130 questions in three languages
一文让你搞懂MYSQL底层原理。-内部结构、索引、锁、集群
Educational Codeforces Round 132 (Rated for Div. 2)【A~C】
How to create a custom 404 error page in WordPress
“index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“