Swift Regex: Beyond the basics

RSS for tag

Discuss the WWDC22 Session Swift Regex: Beyond the basics

Posts under wwdc2022-110358 tag

5 Posts

Post

Replies

Boosts

Views

Activity

How to pass Regex object for use inside SwiftUI View
I am trying to make a textfield that can do its own validation for input using the new Regex APIs. For example, if I pass in the email regex, it can have a green border when its a valid email and red border otherwise. So far I have this: struct MyTextField: View { private var placeholder: String @Binding private var text: String private var regex: (any RegexComponent)? var body: some View { TextField(placeholder, text: $text) .onChange(of: $text) { newValue in guard let regex = regex else { return } if text.matches(of: regex).isEmpty {                 // show invalid state             } else {                 // show valid state             } } } However I get Command SwiftCompile failed with a nonzero exit code due to a type checking error. What is the correct type to use here?
2
0
1k
Feb ’23
Regex case match fails to compile in Xcode 14.0 (14A309)
import RegexBuilder switch "abc" { case /\w+/: print("It's a word!") } Gives an error: expression failed to parse: error: x.xcplaygroundpage:7:6: error: expression pattern of type 'Regex<Substring>' cannot match values of type 'String' case /\w+/: ^ x.xcplaygroundpage:7:6: note: overloads for '~=' exist with these partially matching parameter lists: (Substring, String) case /\w+/: ^
3
1
1.1k
Jan ’23
Swift 5.7 Regex failed to match Foundation URL
Code to reproduce: let inputString = "url(https://example.com)" let regex = Regex {   "url("   Capture {     .url()   }   ")" } guard let (_, url) = inputString.firstMatch(of: regex)?.output else {   fatalError() } print(url) Regex matches if we change "url(" to "(": let regex = Regex {   "("   Capture {     .url()   }   ")" } However, the output is Is this a bug?
0
0
889
Jul ’22
I have a bug in Regex(maybe)
Feedback is filed. Trying to look into the Core.swift. diagnostics: Unsupported: 'Consumer for unconverted(_StringProcessing.DSLTree._AST.Atom(ast:  ---------------------------------------- CrashReportError: Fatal Error in Core.swift UtilityScript crashed due to fatalError in Core.swift at line 77. 'try!' expression unexpectedly raised an error: Unsupported: 'Consumer for unconverted(_StringProcessing.DSLTree._AST.Atom(ast:  ))' Process: UtilityScript[3141] Date/Time: 2022-07-02 03:56:17 +0000 Log File: <none> Application Specific Information:     dyld [         dyld config: DYLD_LIBRARY_PATH=/Users/wangkeshijian/Library/Developer/Xcode/DerivedData/UtilityScript-frovasvohxblobefzbrrwtvqtyuu/Build/Intermediates.noindex/Previews/UtilityScript/Products/Debug DYLD_FRAMEWORK_PATH=/Users/wangkeshijian/Library/Developer/Xcode/DerivedData/UtilityScript-frovasvohxblobefzbrrwtvqtyuu/Build/Intermediates.noindex/Previews/UtilityScript/Products/Debug     ]     libswiftCore.dylib [         _StringProcessing/Core.swift:77: Fatal error: 'try!' expression unexpectedly raised an error: Unsupported: 'Consumer for unconverted(_StringProcessing.DSLTree._AST.Atom(ast:  ))'         /AppleInternal/Library/BuildRoots/0cc5e7ad-e86f-11ec-ac50-3e2aa58faa6a/Library/Caches/com.apple.xbs/Sources/swiftlang_overlay_Platform/swift-experimental-string-processing/Sources/_StringProcessing/ConsumerInterface.swift:200     ]
1
0
877
Jul ’22
Regex Sample Code - Errors with code at [3:49}
Version 14.0 beta (14A5228q) I'm getting errors when using the sample code at [3:49], in a playground. The code is below. The line with the .split throws an error, as does the switch statement. If the parameter named "separator" is replaced with "by", then the line works. Unable to fine a way to get the switch to work. let input = "name: John Appleseed, user_id: 100" let regex = /user_id:\s*(\d+)/ input.firstMatch(of: regex) // Regex.Match<(Substring, Substring)> input.wholeMatch(of: regex) // nil input.prefixMatch(of: regex) // nil input.starts(with: regex) // false input.replacing(regex, with: "456") // "name: John Appleseed, 456" input.trimmingPrefix(regex) // "name: John Appleseed, user_id: 100" input.split(separator: /\s*,\s*/) // ["name: John Appleseed", "user_id: 100"] switch "abc" { case /\w+/: print("It's a word!") }
1
0
1.5k
Jun ’22
How to pass Regex object for use inside SwiftUI View
I am trying to make a textfield that can do its own validation for input using the new Regex APIs. For example, if I pass in the email regex, it can have a green border when its a valid email and red border otherwise. So far I have this: struct MyTextField: View { private var placeholder: String @Binding private var text: String private var regex: (any RegexComponent)? var body: some View { TextField(placeholder, text: $text) .onChange(of: $text) { newValue in guard let regex = regex else { return } if text.matches(of: regex).isEmpty {                 // show invalid state             } else {                 // show valid state             } } } However I get Command SwiftCompile failed with a nonzero exit code due to a type checking error. What is the correct type to use here?
Replies
2
Boosts
0
Views
1k
Activity
Feb ’23
Regex case match fails to compile in Xcode 14.0 (14A309)
import RegexBuilder switch "abc" { case /\w+/: print("It's a word!") } Gives an error: expression failed to parse: error: x.xcplaygroundpage:7:6: error: expression pattern of type 'Regex<Substring>' cannot match values of type 'String' case /\w+/: ^ x.xcplaygroundpage:7:6: note: overloads for '~=' exist with these partially matching parameter lists: (Substring, String) case /\w+/: ^
Replies
3
Boosts
1
Views
1.1k
Activity
Jan ’23
Swift 5.7 Regex failed to match Foundation URL
Code to reproduce: let inputString = "url(https://example.com)" let regex = Regex {   "url("   Capture {     .url()   }   ")" } guard let (_, url) = inputString.firstMatch(of: regex)?.output else {   fatalError() } print(url) Regex matches if we change "url(" to "(": let regex = Regex {   "("   Capture {     .url()   }   ")" } However, the output is Is this a bug?
Replies
0
Boosts
0
Views
889
Activity
Jul ’22
I have a bug in Regex(maybe)
Feedback is filed. Trying to look into the Core.swift. diagnostics: Unsupported: 'Consumer for unconverted(_StringProcessing.DSLTree._AST.Atom(ast:  ---------------------------------------- CrashReportError: Fatal Error in Core.swift UtilityScript crashed due to fatalError in Core.swift at line 77. 'try!' expression unexpectedly raised an error: Unsupported: 'Consumer for unconverted(_StringProcessing.DSLTree._AST.Atom(ast:  ))' Process: UtilityScript[3141] Date/Time: 2022-07-02 03:56:17 +0000 Log File: <none> Application Specific Information:     dyld [         dyld config: DYLD_LIBRARY_PATH=/Users/wangkeshijian/Library/Developer/Xcode/DerivedData/UtilityScript-frovasvohxblobefzbrrwtvqtyuu/Build/Intermediates.noindex/Previews/UtilityScript/Products/Debug DYLD_FRAMEWORK_PATH=/Users/wangkeshijian/Library/Developer/Xcode/DerivedData/UtilityScript-frovasvohxblobefzbrrwtvqtyuu/Build/Intermediates.noindex/Previews/UtilityScript/Products/Debug     ]     libswiftCore.dylib [         _StringProcessing/Core.swift:77: Fatal error: 'try!' expression unexpectedly raised an error: Unsupported: 'Consumer for unconverted(_StringProcessing.DSLTree._AST.Atom(ast:  ))'         /AppleInternal/Library/BuildRoots/0cc5e7ad-e86f-11ec-ac50-3e2aa58faa6a/Library/Caches/com.apple.xbs/Sources/swiftlang_overlay_Platform/swift-experimental-string-processing/Sources/_StringProcessing/ConsumerInterface.swift:200     ]
Replies
1
Boosts
0
Views
877
Activity
Jul ’22
Regex Sample Code - Errors with code at [3:49}
Version 14.0 beta (14A5228q) I'm getting errors when using the sample code at [3:49], in a playground. The code is below. The line with the .split throws an error, as does the switch statement. If the parameter named "separator" is replaced with "by", then the line works. Unable to fine a way to get the switch to work. let input = "name: John Appleseed, user_id: 100" let regex = /user_id:\s*(\d+)/ input.firstMatch(of: regex) // Regex.Match<(Substring, Substring)> input.wholeMatch(of: regex) // nil input.prefixMatch(of: regex) // nil input.starts(with: regex) // false input.replacing(regex, with: "456") // "name: John Appleseed, 456" input.trimmingPrefix(regex) // "name: John Appleseed, user_id: 100" input.split(separator: /\s*,\s*/) // ["name: John Appleseed", "user_id: 100"] switch "abc" { case /\w+/: print("It's a word!") }
Replies
1
Boosts
0
Views
1.5k
Activity
Jun ’22