Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

javascript - Objective-C/Swift regex matching across multliple lines

I have a regular expression originally used in python to extract 2 strings from a scraped HTML page : vars+kaynaklar.*?urls*:s*"([^"]+)"s*,s*datas*:s*'([^']+)

This works fine in Python but not in Objective-C/Swift.

This is the Swift 2.0 code I'm using to find the results:

do {
        let regexString = "var\s+kaynaklar.*?url\s*:\s*"([^"]+)"\s*,\s*data\s*:\s*'([^']+)"
        let regex = try NSRegularExpression(pattern: regexString, 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 []
    }

And this is an example of the JS being checked inside the HTML looks like :

var kaynaklar = [];

jQuery.ajax({
    type:"POST",
    url:"/service/part",
    data:'id=31398',
    success:function(a){
        if(a=="hata")
        {
            jQuery("#player").html("<br><br><font style='color:white;'>Video kayna?? silinmi? lütfen sol üstten Kaynak butonuna t?klayarak farkl? bir kaynak deneyin.</font>")
        }
        else
        {
            for (var i = 1; i < 6; i++) {
                if(a["videolink"+i])
                {
                    kaynaklar.push({"file":a["videolink"+i], "label":a["videokalite"+i],"type":"mp4"});
                }
            }
            video_loader();
        }
    },
    error:function(){
        jQuery("#player").html("<br><br><font style='color:white;'>Video yüklenirken bir hata olu?tu lütfen sayfay? yenileyip tekrar deneyin veya farkl? bir kaynak deneyin.</font>")
    }
})
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From your feedback, I see you just need to tell the regex engine to match a newline with a period.

Use the NSRegularExpressionOptions.DotMatchesLineSeparators option:

Allow . to match any character, including line separators.


Available in OS X v10.7 and later.

As a quicker-to-implement alternative, use an inline (?s) modifier at the beginning of the pattern:

let regexString = "(?s)var\s+kaynaklar.*?url\s*:\s*"([^"]+)"\s*,\s*data\s*:\s*'([^']+)"

See the regex demo.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...