How to restore a regex rule into the json file?
-
When I put the text into the file and used the QJsonObject to parse the data,u know it couldnt work.I knew it maybe was caused by the spcial code what likes the ^$ and so on .But how to fix it ?
{ "context_utils_string_regex_rules":{ "rule_mail_addr_default":"^[A-Za-z0-9-._]+@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,6})$", "rule_mail_addr_server":"@[A-Za-z0-9-]+(\.)", "rule_mail_addr_username":"^[A-Za-z0-9-]*@" } } -
When I put the text into the file and used the QJsonObject to parse the data,u know it couldnt work.I knew it maybe was caused by the spcial code what likes the ^$ and so on .But how to fix it ?
{ "context_utils_string_regex_rules":{ "rule_mail_addr_default":"^[A-Za-z0-9-._]+@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,6})$", "rule_mail_addr_server":"@[A-Za-z0-9-]+(\.)", "rule_mail_addr_username":"^[A-Za-z0-9-]*@" } }@nicker-player
What "does not work"? JSON knows nothing about regular expressions, it only accepts strings. So long as your strings are well formed it will parse them; what you do with them later is a completely separate matter.So what is the problem here? Does the file read into JSON without error, or does it give you a line and error? I haven't looked it up, but maybe JSON requires something for your literal
\s? [EDIT Yep, JSON requires\\for a literal\.] -
@nicker-player
What "does not work"? JSON knows nothing about regular expressions, it only accepts strings. So long as your strings are well formed it will parse them; what you do with them later is a completely separate matter.So what is the problem here? Does the file read into JSON without error, or does it give you a line and error? I haven't looked it up, but maybe JSON requires something for your literal
\s? [EDIT Yep, JSON requires\\for a literal\.]You would need to escape literal double-quotes if they are also present within the regular expressions. Using a JSON package/library, like QJSonDocument, to write the file is the easiest way to ensure that escaping happens where it should (and is reversed on read).
Another observation: your email address regex is not allowing for a lot of options. This, for example, is a valid email address, "someone+something@example.com". See this Stackoverflow article for more.