From: 7stud -- Date: 2011-05-03T03:09:30+09:00 Subject: Re: Formatting a long regex: can a character class [] be split over lines? Alexey Muranov wrote in post #996071: > Hello, > i am wandering if it is possible to split a character class ([...]) in > Ruby regex over multiple lines. > > I know that the /x option allows to ignore whitespace, so i can write : > > email_format = /\A( > [A-Za-z\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]+ > \.)* > [A-Za-z\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]+ > @([a-z\d\-]+\.)+[a-z\d\-]+\z/x > > However, if i try to split inside a character class: > > name_format = /\A[A-Za-z\d > \!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]\z/x > > i get the warning: > > warning: character class has duplicated range > > (apparently it is about the space character being included multiple > times inside []). > I want the space and newlines to be disregarded inside [] to format it > over multiple lines, is this possible? > > Thanks, > > Alexey. 1) Never write a regex with thousands of escapes. Are you aware that inside a character class, the special regex characters lose their special meaning? 2) Break up long regexes into smaller pieces. my_char_class = '[A-Za-z#\d!#$%&\'*+-/=?^_`{|}~]' my_regex = / \A #{my_char_class} [.] #{my_char_class} \z /x if my_regex.match "?./" puts 'yes' end --output:-- yes -- Posted via http://www.ruby-forum.com/.