How to create String split extension with regex in Swift?

snaggs

I wrote extension that create split method:

extension String {
    func split(splitter: String) -> Array<String> {
        return self.componentsSeparatedByString(splitter)
    }
}

So in playground I can write:

var str = "Hello, playground"

if str.split(",").count > 1{
    var out = str.split(",")[0]

    println("output: \(out)") // output: Hello
}

What do I need to make it work with regex like in Java:

str.split("[ ]+")

Because this way it doesn't work.

Thanks,

Mundi

First, your split function has some redundancy. It is enough to return

return self.componentsSeparatedByString(splitter)

Second, to work with a regular expression you just have to create a NSRegularExpression and then perhaps replace all occurrences with your own "stop string" and finally separate using that. E.g.

let regEx = NSRegularExpression.regularExpressionWithPattern
  (splitter, options: NSRegularExpressionOptions(), error: nil)
let stop = "SomeStringThatYouDoNotExpectToOccurInSelf"
let modifiedString = regEx.stringByReplacingMatchesInString
     (self, options: NSMatchingOptions(), 
              range: NSMakeRange(0, countElements(self)), 
       withTemplate:stop)
return modifiedString.componentsSeparatedByString(stop)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to split a string in Swift

From Dev

How to split String without regex

From Dev

How to split String without regex

From Dev

How to split a java string with a regex?

From Dev

How to split a string on regex in Python

From Java

How to split filename from file extension in Swift?

From Dev

How to regex split, but keep the split string?

From Dev

How to split string and create an array?

From Dev

How to create an extension of the Swift struct JoinedSequence?

From Dev

How to Split string In javascript Using regex

From Dev

How to split this string in java need regex?

From Dev

How to split string by java regex with look behind?

From Dev

How to split string using regex in java

From Dev

Regex: How to split and replace a string that contains numbers?

From Dev

How split a string using regex pattern

From Dev

How to split a string into 2 strings using RegEx?

From Dev

How to split a string for regex containing lookbehind and lookahead

From Dev

how to split a string using regex(.match())?

From Dev

how to use regex in python to split a string by ","

From Dev

How to split a string enclosed with "{}" and "[]" using regex expression

From Dev

How can I use regex to split a string by {}

From Dev

How to split a string in c# using Regex?

From Dev

How to create a Regex for string with space

From Dev

how create regex pattern for this string

From Dev

how to create a string like regex

From Dev

Swift: How to add a class method in 'String" extension

From Dev

How to split string using regex to split between +,-,*,/ symbols?

From Dev

How to split string with Regex.Split and keep all separators?

From Dev

How to split a string by length using split function and a RegEx in javascript?

Related Related

  1. 1

    How to split a string in Swift

  2. 2

    How to split String without regex

  3. 3

    How to split String without regex

  4. 4

    How to split a java string with a regex?

  5. 5

    How to split a string on regex in Python

  6. 6

    How to split filename from file extension in Swift?

  7. 7

    How to regex split, but keep the split string?

  8. 8

    How to split string and create an array?

  9. 9

    How to create an extension of the Swift struct JoinedSequence?

  10. 10

    How to Split string In javascript Using regex

  11. 11

    How to split this string in java need regex?

  12. 12

    How to split string by java regex with look behind?

  13. 13

    How to split string using regex in java

  14. 14

    Regex: How to split and replace a string that contains numbers?

  15. 15

    How split a string using regex pattern

  16. 16

    How to split a string into 2 strings using RegEx?

  17. 17

    How to split a string for regex containing lookbehind and lookahead

  18. 18

    how to split a string using regex(.match())?

  19. 19

    how to use regex in python to split a string by ","

  20. 20

    How to split a string enclosed with "{}" and "[]" using regex expression

  21. 21

    How can I use regex to split a string by {}

  22. 22

    How to split a string in c# using Regex?

  23. 23

    How to create a Regex for string with space

  24. 24

    how create regex pattern for this string

  25. 25

    how to create a string like regex

  26. 26

    Swift: How to add a class method in 'String" extension

  27. 27

    How to split string using regex to split between +,-,*,/ symbols?

  28. 28

    How to split string with Regex.Split and keep all separators?

  29. 29

    How to split a string by length using split function and a RegEx in javascript?

HotTag

Archive