質問編集履歴

2

修正

2023/01/03 06:36

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -23,9 +23,8 @@
23
23
  print("-------")
24
24
 
25
25
  // nextResults
26
- type(of: nextResults) // Expression of type 'Any.Type' is unused
26
+ let res = nextResults.match(#"max_id(([0-9]+):\d+)&"#)
27
- let res = (nextResults as AnyObject).match(#"--<(([a-zA-Z]+):\d+)>--"#) // Value of type 'AnyObject' has no member 'match'
27
+ print(res)
28
-
29
28
 
30
29
  // オーバーライド:正規表現
31
30
  extension String {
@@ -45,8 +44,12 @@
45
44
  ```
46
45
 
47
46
  ```
47
+ -------
48
48
  nextResultsは
49
49
  ?max_id=1609520488455499776&q=%23%E4%B8%83%E5%91%B3%E5%94%90%E8%BE%9B%E5%AD%90&include_entities=1
50
+ -------
51
+ []
52
+
50
53
  ```
51
54
 
52
55
  ### 試したこと

1

編集

2023/01/03 06:15

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -15,21 +15,48 @@
15
15
  ### 該当のソースコード
16
16
 
17
17
  ```Swift
18
+ import Foundation
19
+
18
- print("-------")
20
+ print("-------")
19
- print("result[next_results]は")
21
+ print("nextResultsは")
20
- print(result["next_results"] as Any)
22
+ print(nextResults)
21
- print("-------")
23
+ print("-------")
24
+
25
+ // nextResults
26
+ type(of: nextResults) // Expression of type 'Any.Type' is unused
27
+ let res = (nextResults as AnyObject).match(#"--<(([a-zA-Z]+):\d+)>--"#) // Value of type 'AnyObject' has no member 'match'
28
+
29
+
30
+ // オーバーライド:正規表現
31
+ extension String {
32
+ func match(_ pattern: String) -> [String] {
33
+ let range = NSRange(location: 0, length: self.count)
34
+ guard let regex = try? NSRegularExpression(pattern: pattern),
35
+ let matched = regex.firstMatch(in: self, range: range)
36
+ else { return [] }
37
+ return (0 ..< matched.numberOfRanges).compactMap { i in
38
+ let r = matched.range(at: i)
39
+ if r.location == NSNotFound { return nil }
40
+ return NSString(string: self).substring(with: r)
41
+ }
42
+ }
43
+ }
44
+
22
45
  ```
23
46
 
24
47
  ```
25
- result[next_results]
48
+ nextResultsは
26
- Optional(?max_id=1609453661192880127&q=%23%E4%B8%83%E5%91%B3%E5%94%90%E8%BE%9B%E5%AD%90&include_entities=1)
49
+ ?max_id=1609520488455499776&q=%23%E4%B8%83%E5%91%B3%E5%94%90%E8%BE%9B%E5%AD%90&include_entities=1
27
50
  ```
28
51
 
29
52
  ### 試したこと
30
53
 
31
- ⚫︎Swift3で特定の文字列の中から数字を抽出する方法
54
+ ~~⚫︎Swift3で特定の文字列の中から数字を抽出する方法
32
- https://qiita.com/kakky0418/items/67d60143b8babc7cbbef
55
+ https://qiita.com/kakky0418/items/67d60143b8babc7cbbef~~
56
+
57
+ 最近の試した記事
58
+ ⚫︎Swift: 正規表現を使って文字列を抜き出す(グループ化対応)
59
+ https://zenn.dev/kyome/articles/1a55547614dd495a869d
33
60
 
34
61
  正規表現関連の記事を見て一通り試してみました。
35
62
  数字のみの抽出の記事がなく、含有の判定のみでした。