Sunday 28 May 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
PCRE regex 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)
}
}

Answer



Just add the (?m) at the start of your pattern.




let locationRegex = "(?m)^LOCATION:(.*(?:\\n :?.*)*)"
^^^^


The (?m) is the inline modifier version of the MULTILINE modifier that forces ^ to match the location at the beginning of a line rather than string (and makes $ match the location at the end of the line, before the newline sequence).



See Table 4: Flag Options:




m flag:
Control the behavior of ^ and $ in a pattern. By default these will only match at the start and end, respectively, of the input text. If this flag is set, ^ and $ will also match at the start and end of each line within the input text.




No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...