快速的正则表达式

时间:2019-05-18 02:49:45

标签: swift regex regex-group regex-greedy nsregularexpression

我很快被NSRegularExpression弄糊涂了,有人可以帮助我吗?

任务:1 已赋予("name","john","name of john")
然后我应该得到["name","john","name of john"]。在这里,我应该避免使用括号。

任务:2 已赋予("name"," john","name of john")
然后我应该得到["name","john","name of john"]。在这里,我应该避免使用括号和多余的空格,并最终获得字符串数组。

任务:3 已赋予key = value // comment
然后我应该得到["key","value","comment"]。在这里,我应该避免=//来获取字符串
我已经尝试了下面的任务1的代码,但是没有通过。

let string = "(name,john,string for user name)"
let pattern = "(?:\\w.*)"

do {
    let regex = try NSRegularExpression(pattern: pattern, options: .caseInsensitive)
    let matches = regex.matches(in: string, options: [], range: NSRange(location: 0, length: string.utf16.count))
    for match in matches {
        if let range = Range(match.range, in: string) {
            let name = string[range]
            print(name)
        }
    }
} catch {
    print("Regex was bad!")
}


预先谢谢。

5 个答案:

答案 0 :(得分:1)

用空格以外的非字母数字字符分隔字符串。然后用空格修剪元素。

extension String {
    func words() -> [String] {
        return self.components(separatedBy: CharacterSet.alphanumerics.inverted.subtracting(.whitespaces))
                .filter({ !$0.isEmpty })
                .map({ $0.trimmingCharacters(in: .whitespaces) })
    }
}

let string1 = "(name,john,string for user name)"
let string2 = "(name,       john,name of john)"
let string3 = "key = value // comment"

print(string1.words())//["name", "john", "string for user name"]
print(string2.words())//["name", "john", "name of john"]
print(string3.words())//["key", "value", "comment"]

答案 1 :(得分:1)

让我们考虑一下:

let string = "(name,José,name is José)"

我建议使用正则表达式查找字符串,其中:

  • 它是完整字符串开头(之后或逗号之后的子字符串,即在(?<=^\(|,)的断言后面查找;
  • 这是其中不包含,的子字符串,即[^,]+?;
  • 该子字符串以逗号或)终止于完整字符串的末尾,即,预先声明(?=,|\)$)的断言,并且
  • 如果您想让它在子字符串前后跳过空格,请同时输入\s*+

因此:

let pattern = #"(?<=^\(|,)\s*+([^,]+?)\s*+(?=,|\)$)"#
let regex = try! NSRegularExpression(pattern: pattern)
regex.enumerateMatches(in: string, range: NSRange(string.startIndex..., in: string)) { match, _, _ in
    if let nsRange = match?.range(at: 1), let range = Range(nsRange, in: string) {
        let substring = String(string[range])
        // do something with `substring` here
    }
}

请注意,我正在使用Swift 5扩展字符串定界符(以#"开头,以"#结尾),这样我就不必在字符串中转义反斜杠。如果您使用的是Swift 4或更早版本,则需要转义那些反斜杠:

let pattern = "(?<=^\\(|,)\\s*+([^,]+?)\\s*+(?=,|\\)$)"

答案 2 :(得分:0)

Swift中的RegEx

这些文章可能有助于您快速探索正则表达式:

任务1和2

此表达式可以帮助您匹配任务1和2的所需输出。

"(\s+)?([a-z\s]+?)(\s+)?"

enter image description here


根据Rob的建议,您可以大大减少边界,例如字符列表[a-z\s]。例如,在这里,我们还可以使用:

"(\s+)?(.*?)(\s+)?"

"(\s+)?(.+?)(\s+)?"

只需在两个和/或空格之间传递所有内容。

enter image description here

RegEx

如果这不是您想要的表达式,则可以在regex101.com中修改/更改表达式。

RegEx电路

您还可以在jex.im中可视化您的表达式:

enter image description here

JavaScript演示

const regex = /"(\s+)?([a-z\s]+?)(\s+)?"/gm;
const str = `"name","john","name of john"
"name","       john","name of john"
"       name  ","       john","name of john     "
"       name  ","       john","       name of john     "`;
const subst = `\n$2`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);

任务3

This expression可以帮助您设计第三个任务的表达式:

(.*?)([a-z\s]+)(.*?)

enter image description here

const regex = /(.*?)([a-z\s]+)(.*?)/gm;
const str = `key = value // comment
key = value with some text // comment`;
const subst = `$2,`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);

答案 3 :(得分:0)

单个模式可用于测试:1 ... 3,在Swift中。

let string =
    //"(name,john,string for user name)" //test:1
    //#"("name","       john","name of john")"# //test:2
    "key = value // comment" //test:3

let pattern = #"(?:\w+)(?:\s+\w+)*"# //Swift 5+ only
//let pattern = "(?:\\w+)(?:\\s+\\w+)*"

do {
    let regex = try NSRegularExpression(pattern: pattern)
    let matches = regex.matches(in: string, range: NSRange(0..<string.utf16.count))
    let matchingWords = matches.map {
        String(string[Range($0.range, in: string)!])
    }
    print(matchingWords) //(test:3)->["key", "value", "comment"]
} catch {
    print("Regex was bad!")
}

答案 4 :(得分:0)

在理解了以上所有评论之后,我在这里做了。

let text = """
Capturing and non-capturing groups are somewhat advanced topics. You’ll encounter examples of capturing and non-capturing groups later on in the tutorial
"""

extension String {
            func  rex (_ expr : String)->[String] {
                return try! NSRegularExpression(pattern: expr, options: [.caseInsensitive])
                .matches(in: self, options: [], range: NSRange(location: 0, length: self.count))
                    .map {
                        String(self[Range($0.range, in: self)!])
                }
            }
        }
let r = text.rex("(?:\\w+-\\w+)") // pass any rex