Sign in
Log inSign up

How do you wrap a long regex?

Marco Alka's photo
Marco Alka
·Jul 3, 2017

Say you have a long regex, like the one for RFC5322 (email address). It is basically just a one-line-assignment operation to compile that monster, but it feels like that line is over nine thousand characters long. How would you line-wrap such a regex? Would you even care to wrap it?

// RFC5322
// regex taken from regular-expressions.info/email.html
Pattern cachedEmailRFCChecker = Pattern.compile("^\\A(?=[a-z0-9@.!#$%&'*+\\/=?^_`\\{|\\}~-]{6,254}\\z)(?=[a-z0-9.!#$%&'*+\\/=?^_`\\{|\\}~-]{1,64}@)[a-z0-9!#$%&'*+\\/=?^_`\\{|\\}~-]+(?:\\.[a-z0-9!#$%&'*+\\/=?^_`\\{|\\}~-]+)*@(?:(?=[a-z0-9-]{1,63}\\.)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+(?=[a-z0-9-]{1,63}\\z)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\z$");