Tuesday, 10 October 2017

regex - Why is my line anchor not functioning as I expected?

I'm trying to parse the location entries from a .ics file
as follows seen in the image below . Normally this works fine even though the only
tester I can find is PCRE rather than ICU as I just need to add an extra backslash to
any special characters.



However in swift I get
the following results:



"^Location"
//No Match

"^VERSION" //No Match
"^BEGIN"
//Match
"^ :"
//Match


Can I get the
'^' anchors to function like they do in the PCRE
tester?



PCRE regex
tester

rel="nofollow noreferrer">PCRE<br />            regex<br />            tester




Code



func
testParticipantParsing()
{
//let locationRegex =
"^LOCATION:(.*(?:\\n :?.*)*)"
let locationRegex = "LOCATION:(.*(?:\\n
:?.*)*)"
var regex : NSRegularExpression! = nil
var resultsArray =
[String]()

//Parse for location

do

{
regex = try NSRegularExpression(pattern: locationRegex, options:
NSRegularExpressionOptions.init(rawValue: 0))
let nsString = content as
NSString
let results = regex.matchesInString(content, options: [], range:
NSMakeRange(0, nsString.length))
resultsArray = results.map{
nsString.substringWithRange($0.range) }
}
//Catch errors if regex
fails
catch
{

print("invalid
regex")
}
//Strip .ics new line tokens
for var result
in resultsArray
{
result =
result.stringByReplacingOccurrencesOfString("\n :", withString: "")
result =
result.stringByReplacingOccurrencesOfString("\n ", withString: "")

print(result)

}
}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...