Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Swift中搜索以\n\n开头的字符串_Ios_Regex_Swift - Fatal编程技术网

Ios 在Swift中搜索以\n\n开头的字符串

Ios 在Swift中搜索以\n\n开头的字符串,ios,regex,swift,Ios,Regex,Swift,嘿,我有一个要求,增加我的UILables中的间距,用于双倍间距的换行符。我想搜索我的字符串并找到所有以\n\n开头的字符串。例如“Hello world\n我在下一行\n\n现在我在下一行,并且间隔比以前大\n现在我回到正常间隔”。我很难找出这个的正则表达式。我正在努力: let regExRule = "^\n\n*" 并将其传递给此函数: func matchesForRegexInText(regex: String, text: String) -> [String] {

嘿,我有一个要求,增加我的UILables中的间距,用于双倍间距的换行符。我想搜索我的字符串并找到所有以
\n\n
开头的字符串。例如“
Hello world\n我在下一行\n\n现在我在下一行,并且间隔比以前大\n现在我回到正常间隔”
。我很难找出这个的正则表达式。我正在努力:

let regExRule = "^\n\n*"
并将其传递给此函数:

func matchesForRegexInText(regex: String, text: String) -> [String] {

        do {
            let regex = try NSRegularExpression(pattern: regex, options: [])
            let nsString = text as NSString
            let results = regex.matchesInString(text,
                                                options: [], range: NSMakeRange(0, nsString.length))
            return results.map { nsString.substringWithRange($0.range)}
        } catch let error as NSError {
            print("invalid regex: \(error.localizedDescription)")
            return []
        }
    }

但是,我得到一个空数组。我真的不知道如何为此构造正则表达式模式。任何指点都将不胜感激。谢谢

我看到的主要问题是正则表达式模式应该包括一个捕获组来选择所需的多个字符串

func matchesForRegexInText(regex : String, text: String) -> [String] {
    var captured = [String]()
    let exp = try! NSRegularExpression(pattern: regex, options: [])
    let matches = exp.matchesInString(text, options:[], range: NSMakeRange(0, text.characters.count))

    for match in matches {
        let c = (text as NSString).substringWithRange(match.rangeAtIndex(1))
        captured.append(c)
    }

    return captured
}

let re = "\\n\\n([\\w\\\\s,']+)"; // selection with (...)

// ["Alpha", "Bravo", "Charlie"]
let strResults = matchesForRegexInText(re, text: "\n\nAlpha\n\nBravo\n\nCharlie\n\n")

我看到的主要问题是正则表达式模式应该包括一个捕获组来选择所需的多个字符串

func matchesForRegexInText(regex : String, text: String) -> [String] {
    var captured = [String]()
    let exp = try! NSRegularExpression(pattern: regex, options: [])
    let matches = exp.matchesInString(text, options:[], range: NSMakeRange(0, text.characters.count))

    for match in matches {
        let c = (text as NSString).substringWithRange(match.rangeAtIndex(1))
        captured.append(c)
    }

    return captured
}

let re = "\\n\\n([\\w\\\\s,']+)"; // selection with (...)

// ["Alpha", "Bravo", "Charlie"]
let strResults = matchesForRegexInText(re, text: "\n\nAlpha\n\nBravo\n\nCharlie\n\n")

您希望字符串介于
\n
或两个
\n\n
之间,因为两个
\n\n
只出现一次。严格来说,在示例字符串中,两个换行符中没有包含任何字符串。顺便说一下:在这种情况下,将参数声明为隐式展开选项是毫无意义的。@vadian你说得对。问题编辑。@vadian:关于IUO:我可能要为此负责。这个函数(最初在这个问题中发布)是我在这里回答的,我从问题中复制了IUO,没有考虑它。也许Swift 1.2中有原因,我不记得了。无论如何,我现在已经解决了这个问题。您希望字符串介于
\n
或两个
\n\n
之间,因为两个
\n\n
只出现一次。严格地说,在示例字符串中,没有包含在两个换行符中的字符串。顺便说一下:在这种情况下,将参数声明为隐式展开选项是毫无意义的。@vadian你说得对。问题编辑。@vadian:关于IUO:我可能要为此负责。这个函数(最初在这个问题中发布)是我在这里回答的,我从问题中复制了IUO,没有考虑它。也许Swift 1.2中有原因,我不记得了。不管怎样,我现在已经解决了这个问题。我更新了我的示例。我误解了你的问题,以为你用的是文字
\n
字符而不是新行。我认为更新会有所帮助。我更新了我的示例。我误解了你的问题,以为你用的是文字
\n
字符而不是新行。我认为更新会有所帮助。