Meet Swift Regex

RSS for tag

Discuss the WWDC22 Session Meet Swift Regex

Posts under wwdc2022-110357 tag

9 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 Error Thread 1: EXC_BREAKPOINT (code=1, subcode=0x22a78b848)
I run into Thread 1: EXC_BREAKPOINT Error, This code should be right. import Foundation import RegexBuilder let string = "https://swift.org, #swift, @swift" let regex = Regex {   ChoiceOf {     OneOrMore(.url())     WordWithPrefix(prefix: "@")     WordWithPrefix(prefix: "#")   } } struct WordWithPrefix: RegexComponent {   let prefix: String   @RegexComponentBuilder   var regex: Regex<some RegexComponent> {     prefix     OneOrMore(.word)   } } for match in string.matches(of: regex) { // Thread 1: EXC_BREAKPOINT (code=1, subcode=0x22a78b848)   print(String(string[match.range])) }
1
0
1.4k
Aug ’22
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
876
Jul ’22
Regex code from WWDC sample code not working
If I past the samele code import RegexBuilder let fieldSeparator = /\s{2,}|\t/ let transactionMatcher = Regex { /CREDIT|DEBIT/ fieldSeparator One(.date(.numeric, locale: Locale(identifier: "en_US"), timeZone: .gmt)) fieldSeparator OneOrMore { NegativeLookahead { fieldSeparator } CharacterClass.any } fieldSeparator One(.localizedCurrency(code: "USD").locale(Locale(identifier: "en_US"))) } It does not like the OneOrMore with the negative look ahead. Is there an updated example of this code from WWDC?
1
1
1.3k
Jun ’22
Regex expression in .split generates cannot convert type
Using Version 14.0 beta (14A5228q) on Monterey 12.4 I am trying to follow the 'Meet Swift regex' presentation dated 7 June but I have stumbled on the most basic of errors that I cannot get around. At 3:56 in the video, the following line of code is shown let transaction = "DEBIT 03/05/2022 Doug's Dugout Dogs $33.27" let fragments = transaction.split(separator: /\s{2,}|\t/) // ["DEBIT", "03/05/2022", "Doug's Dugout Dogs", "$33.27"]`` The '/' of the separator string generates the error: error: cannot convert value of type 'Regex' to expected argument type 'String.Element' (aka 'Character') I am embarrassed but also stuck on the simple issue and I cannot figure out what I am missing. TIA Chris
2
0
2.9k
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
I want to use ForEach in RegexBuilder like SwiftUI
let words = ["@apple", "#swift"] let regex = Regex {   ChoiceOf {     ForEach(words, id: \.self) { word in       OneOrMore(word)     }   } }
Replies
1
Boosts
0
Views
1.1k
Activity
Aug ’22
Regex Error Thread 1: EXC_BREAKPOINT (code=1, subcode=0x22a78b848)
I run into Thread 1: EXC_BREAKPOINT Error, This code should be right. import Foundation import RegexBuilder let string = "https://swift.org, #swift, @swift" let regex = Regex {   ChoiceOf {     OneOrMore(.url())     WordWithPrefix(prefix: "@")     WordWithPrefix(prefix: "#")   } } struct WordWithPrefix: RegexComponent {   let prefix: String   @RegexComponentBuilder   var regex: Regex<some RegexComponent> {     prefix     OneOrMore(.word)   } } for match in string.matches(of: regex) { // Thread 1: EXC_BREAKPOINT (code=1, subcode=0x22a78b848)   print(String(string[match.range])) }
Replies
1
Boosts
0
Views
1.4k
Activity
Aug ’22
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
876
Activity
Jul ’22
Do iPadOS 16 Playground support RegxBuilder?
It doesn’t seems to
Replies
4
Boosts
0
Views
1.3k
Activity
Jun ’22
Lookbehind support in Swift Regex?
There is a Lookahead struct, but no Lookbehind struct. Is there a way I can get lookbehinds / negative lookbehinds with Swift Regex?
Replies
0
Boosts
1
Views
1.1k
Activity
Jun ’22
Regex code from WWDC sample code not working
If I past the samele code import RegexBuilder let fieldSeparator = /\s{2,}|\t/ let transactionMatcher = Regex { /CREDIT|DEBIT/ fieldSeparator One(.date(.numeric, locale: Locale(identifier: "en_US"), timeZone: .gmt)) fieldSeparator OneOrMore { NegativeLookahead { fieldSeparator } CharacterClass.any } fieldSeparator One(.localizedCurrency(code: "USD").locale(Locale(identifier: "en_US"))) } It does not like the OneOrMore with the negative look ahead. Is there an updated example of this code from WWDC?
Replies
1
Boosts
1
Views
1.3k
Activity
Jun ’22
Regex expression in .split generates cannot convert type
Using Version 14.0 beta (14A5228q) on Monterey 12.4 I am trying to follow the 'Meet Swift regex' presentation dated 7 June but I have stumbled on the most basic of errors that I cannot get around. At 3:56 in the video, the following line of code is shown let transaction = "DEBIT 03/05/2022 Doug's Dugout Dogs $33.27" let fragments = transaction.split(separator: /\s{2,}|\t/) // ["DEBIT", "03/05/2022", "Doug's Dugout Dogs", "$33.27"]`` The '/' of the separator string generates the error: error: cannot convert value of type 'Regex' to expected argument type 'String.Element' (aka 'Character') I am embarrassed but also stuck on the simple issue and I cannot figure out what I am missing. TIA Chris
Replies
2
Boosts
0
Views
2.9k
Activity
Jun ’22