Parsing swift2相当于python';s shlex.split以保留带引号字符串中的空白

Parsing swift2相当于python';s shlex.split以保留带引号字符串中的空白,parsing,swift2,shlex,Parsing,Swift2,Shlex,我正在寻找一个现有的swift2函数来拆分空白字符串输入,同时保留带引号字符串中的空白 我读过。我的问题似乎没有重复 我在cocoapods中搜索类似的功能。我没有找到它 如果swift2中不存在这个shlex.split函数,那么实现类似功能的有效替代方法是什么?在保留内部带引号字符串中的空白的同时拆分字符串的另一种方法是什么 下面是我在python中的意思示例: $ python Python 2.7.6 (default, Jun 22 2015, 18:00:18) [GCC 4

我正在寻找一个现有的swift2函数来拆分空白字符串输入,同时保留带引号字符串中的空白

我读过。我的问题似乎没有重复

我在cocoapods中搜索类似的功能。我没有找到它

如果swift2中不存在这个shlex.split函数,那么实现类似功能的有效替代方法是什么?在保留内部带引号字符串中的空白的同时拆分字符串的另一种方法是什么

下面是我在python中的意思示例:

$    python
Python 2.7.6 (default, Jun 22 2015, 18:00:18) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import shlex
>>> input=""" alpha 2 'chicken with teeth' 4 'cat with wings' 6 turkey"""
>>> results = shlex.split(input)
>>> type(results)
<type 'list'>
>>> results[0]
'alpha'
>>> results[2]
'chicken with teeth'
>>> for term in results:
...     print(term)
... 
alpha
2
chicken with teeth
4
cat with wings
6
turkey
>>> 
$python
Python 2.7.6(默认值,2015年6月22日,18:00:18)
[GCC 4.8.2]关于linux2
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>导入shlex
>>>输入=“阿尔法2”有牙齿的鸡“4”有翅膀的猫“6火鸡”
>>>结果=shlex.split(输入)
>>>类型(结果)
>>>结果[0]
“阿尔法”
>>>结果[2]
“有牙鸡”
>>>对于结果中的术语:
...     印刷品(学期)
... 
阿尔法
2.
有牙鸡
4.
有翅膀的猫
6.
火鸡
>>> 

正如@EricD在给您的评论中所写的那样,不存在这样的本机Swift函数。但是,您可以很容易地编写自己的此类拆分函数,例如

extension String {

    func shlexSplit() -> [String] {
        /* separate words by spaces */
        var bar = self.componentsSeparatedByString(" ")

        /* identify array idx ranges of quoted (') sets of words */
        var accumulating = false
        var from = 0
        var joinSegments : [(Int, Int)] = []

        for (i,str) in bar.enumerate() {
            if str.containsString("'") {
                if accumulating { joinSegments.append((from, i)) }
                else { from = i }
                accumulating = !accumulating
            }
        }

        /* join matching word ranges with " " */
        for (from, through) in joinSegments.reverse() {
            bar.replaceRange(from...through, 
                with: [bar[from...through].joinWithSeparator(" ")])
        }

        return bar
    }
}
用法示例

/* exampe usage */
let foo = "alpha 2 'chicken with teeth' 4 'cat with wings' 6 turkey"
let bar = foo.shlexSplit()

bar.forEach{ print($0) }
/* alpha
 2
 'chicken with teeth'
 4
 'cat with wings'
 6
 turkey */

请注意,上面假设输入字符串具有匹配的引号分隔符集。

正如@EricD在其给您的注释中所写,不存在此类本机Swift函数。但是,您可以很容易地编写自己的此类拆分函数,例如

extension String {

    func shlexSplit() -> [String] {
        /* separate words by spaces */
        var bar = self.componentsSeparatedByString(" ")

        /* identify array idx ranges of quoted (') sets of words */
        var accumulating = false
        var from = 0
        var joinSegments : [(Int, Int)] = []

        for (i,str) in bar.enumerate() {
            if str.containsString("'") {
                if accumulating { joinSegments.append((from, i)) }
                else { from = i }
                accumulating = !accumulating
            }
        }

        /* join matching word ranges with " " */
        for (from, through) in joinSegments.reverse() {
            bar.replaceRange(from...through, 
                with: [bar[from...through].joinWithSeparator(" ")])
        }

        return bar
    }
}
用法示例

/* exampe usage */
let foo = "alpha 2 'chicken with teeth' 4 'cat with wings' 6 turkey"
let bar = foo.shlexSplit()

bar.forEach{ print($0) }
/* alpha
 2
 'chicken with teeth'
 4
 'cat with wings'
 6
 turkey */
请注意,上面假设输入字符串具有匹配的引号分隔符集。

'pure'swift(无基础)示例

用法

“纯”swift(无基础)示例

用法


我正在寻找一个现有的swift2函数到[…]
Swift标准库中没有这样的函数。顺便说一句,这个函数相当复杂,它使用词法分析器来完成这个任务。
我正在寻找一个现有的swift2函数到[…]
Swift标准库中没有这样的函数。顺便说一句,这个函数相当复杂,它使用词法分析器来完成这项任务。