Questions tagged [swift-regexbuilder]

11 questions
3
votes
1 answer

How to build a ChoiceOf regex component from an CaseIterable enumeration of String values?

Currently I use this workaround to pass a list of enum cases to a ChoiceOf. enum Fruit: String, CaseIterable { case apple = "Apple" case banana = "Banana" case strawberry = "Strawberry" } let regex = Regex { ChoiceOf { try!…
parapote
  • 433
  • 2
  • 8
3
votes
1 answer

How to capture more than 10 things using Swift 5.7's RegexBuilder?

Let's say I have a file that stores information about people, and one of the lines look like this: Sweeper 30 1992-09-22 China/Beijing - 0 2020-07-07 Mary/Linda - Pizza/Lemon From left to right, it's name, age, date of birth, country of birth, city…
Sweeper
  • 213,210
  • 22
  • 193
  • 313
2
votes
0 answers

Swift 5.7 RegexBuilder convert Array of strings to Regex - ChoiceOf options programatically

I would love to take an array of strings let array = ["one", "two", "three", "four"] and convert it to the regex builder equivalent of: Regex { ChoiceOf{ "one" "two" "three" "four" } } or basically the equivelant…
The Fox
  • 1,189
  • 1
  • 6
  • 10
1
vote
0 answers

How to convert a Swift 5.7 Regex back to a string?

It's pretty straightforward to convert a string to a regex: let regex = try! Regex("foo") But if I try to get the string back out of the regex, instead of foo, I get something…
Bbrk24
  • 739
  • 6
  • 22
1
vote
1 answer

How to create a Swift Regex that outputs a custom type?

In the WWDC videos, it was shown that you can do something like this with Captures/TryCaptures in the Regex Builder: let regex = Regex { // ... TryCapture { OneOrMore(.digit) } transform: { Int($0) } // ... } And the output of…
Sweeper
  • 213,210
  • 22
  • 193
  • 313
0
votes
2 answers

Problems creating regex for not containing character

I want to act on markdown strings depending on whether they start with one, two or no #, but I am failing to rule ## out for being recognized as a title: let strings = ["# Fancy Title", "## Fancy Subtitle", "Body Content") for string in strings { …
appfrosch
  • 1,146
  • 13
  • 36
0
votes
0 answers

Swift 5.7 RegexBuilder: Nested TryCapture - transform / Mapping Output?

Here in this example I tried to capture two Int values and then capture them together as a struct. This gives a "Thread 1: signal SIGABRT" error. (NOTE: I know that my example could be fixed by simply not nesting the Captures and handling the…
The Fox
  • 1,189
  • 1
  • 6
  • 10
0
votes
1 answer

Swift RegexBuilder new syntax 2 or more spaces

Hi I'm trying to build a regular expression builder to detect 2 or more spaces or a tab, so (let twoOrMoreSpacesOrTab = /\s{2,}|\t/) How to build this using a Regex Builder? I tried this but its not 100% acurate: ChoiceOf { OneOrMore(" ") …
Godfather
  • 4,040
  • 6
  • 43
  • 70
0
votes
0 answers

Possible to configure Swift RegexBuilder DSL Capture '.url' to work with a Markdown link?

Is it possible to configure the Swift RegexBuilder DSL Capture .url(…) to precisely capture the URL within standard Markdown link syntax? If yes, then how? Minimal Pattern Attemp let inputMD = "[Markdown link text](https://example.com)" let regexMD…
marc-medley
  • 8,931
  • 5
  • 60
  • 66
0
votes
2 answers

Access an optional capture by name when using Swift Regex Builder

I'm just getting started with regular expressions and Swift Regex, so a heads up that my terminology my be incorrect. I have boiled this problem down to a very simple task: I have input lines that have either just one word (a name) or start with the…
rene
  • 1,975
  • 21
  • 33
0
votes
1 answer

How to use Swift 5.7 RegexBuilder to find a word after a sentence

I get a report from networksetup -listnetworkserviceorder terminal command for all networks services. The returned text is like this: An asterisk (*) denotes that a network service is disabled. (1) Ethernet (Hardware Port: Ethernet, Device:…
Hadi Sharghi
  • 903
  • 16
  • 33