GNOME Bugzilla – Bug 749576
'\R' is invalid escape sequence
Last modified: 2018-05-14 08:33:34 UTC
The PCRE (used in GLib for Regex) is encouraging to use '\R' escape sequence, but valac doesn't allow it. Valadoc also has it: http://references.valadoc.org/#!api=glib-2.0/GLib.Regex This code will not compile: void main() { var myregex = new Regex("\R"); } The compiler says: error: invalid escape sequence var myregex = new Regex("\R"); ^ Well, '\R' is not on the list of supported escape sequences in valascanner.vala, maybe it is a good idea to add it?
There is a difference between a string escape sequence and a PCRE pattern. For example if you want to print a newline: print( "your text followed by a new line\n" ); In PCRE \R matches the pattern for any Unicode newline sequence. So a print statement such as: print( "your text followed by a Unicode newline sequence\R" ); doesn't make too much sense because it could be translated to one of a number of newline sequences. If you wish to pass \R to the PCRE engine escape the \, e.g. var myregex = new Regex("\\R"); or use a verbatim string (see https://wiki.gnome.org/Projects/Vala/Tutorial#Strings), e.g.: var myregex = new Regex("""\R"""); If you are looking for a faster implementation use regular expression literals, e.g.: if ( /\\R/.match( test_string ) ) { print ( "matched" ); } These can be 3x-4x faster, See https://bugzilla.gnome.org/attachment.cgi?id=157022&action=edit
Created attachment 303604 [details] Test case for regex literal with \R unicode newline match
Seems like regex literals have a problem with \R. The attached test case will not compile. The test produces the error: regex_literal_uncode_newline_match_test.vala:4.13-4.13: error: invalid escape sequence Changing the code to use: assert ( /\\Rnext/.match( a ) == true ); compiles, but fails the assertion with: ERROR:/home/al/software_projects/regex_literal_uncode_newline_match_test.vala.c:42:_vala_main: assertion failed: (/\\Rnext/.match( a ) == true)
Created attachment 371988 [details] [review] scanner: Accept \R and \N escape sequences in regex literals
Attachment 371988 [details] pushed as b22d43f - scanner: Accept \R and \N escape sequences in regex literals