Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...