swift - Return results from regular expression pattern matching -


i have string (html in example case) contains same pattern displaying results of sports games. so, html tags known, values each game not.

in perl, can this:

if ( $content =~ /\<\/a\>\<br\>(\d+)\<\/span\>\<br\>(\d+)\-(\d+).+\<\/a\>\<br\>(\d+)\<\/span\>\<br\>(\d+)\-(\d+)/) {   $visitingteamscore = $1; // $1 1st matched digit   $visitingteamwins = $2; // $2 2nd matched digit   $visitingteamlosses = $3; // etc   $hometeamscore = $4;   $hometeamwins = $5;   $hometeamlosses = $6; } 

which returns digits inside parentheses, in case 6 total integers of varying digit lengths. can assign matches variables.


from answer in question: swift string between 2 strings in string, have following swift code:

extension string {     func slicefrom(start: string, to: string) -> string? {         return (rangeofstring(start)?.endindex).flatmap { sind in             (rangeofstring(to, range: sind..<endindex)?.startindex).map { eind in                 substringwithrange(sind..<eind)             }         }     } }  let firstmatch = content?.slicefrom("</a><br>", to: "</span>") // first integer in string 

the problem comes in when getting 4th integer between </a\><br> , </span> resulting match first digit again.

i can manually count characters (which isn't perfect science because digits in each integer can differ) ugly like:

let newrawhtml = content![content!.startindex.advancedby(15)...content!.startindex.advancedby(5)] 

another possibility remove matched string, making shorter each subsequent search (which i'm not sure how implement.) what's way this? there way in swift "pluck out" matches?

the code have shown perl example, uses regular expression. , in case pattern getting little bit complex, you'd better use nsregularexpression directly.

let pattern = "</a><br>(\\d+)</span><br>(\\d+)-(\\d+).+</a><br>(\\d+)</span><br>(\\d+)-(\\d+)" let regex = try! nsregularexpression(pattern: pattern, options: []) if let match = regex.firstmatchinstring(content, options: [], range: nsrange(0..<content.utf16.count)) {     let visitingteamscore = (content nsstring).substringwithrange(match.rangeatindex(1))     let visitingteamwins = (content nsstring).substringwithrange(match.rangeatindex(2))     let visitingteamlosses = (content nsstring).substringwithrange(match.rangeatindex(3))     let hometeamscore = (content nsstring).substringwithrange(match.rangeatindex(4))     let hometeamwins = (content nsstring).substringwithrange(match.rangeatindex(5))     let hometeamlosses = (content nsstring).substringwithrange(match.rangeatindex(6))     //...use values } 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

python 3.5 - Pyqtgraph string in x tick -