Formatting a long regex: can a character class [] be split overlines?

A

Alexey Muranov

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.
 
7

7stud --

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
 
7

7stud --

It's also possible to escape a newline:

name_format = /\A[A-Za-z\d\
\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]\z/x

but then you can't indent the second line or else your regex will
contain a bunch of spaces.
 
7

7stud --

You could also use a here doc and avoid having to escape any character
inside the string:

str = <<'LOTS_OF_SYMBOLS'
[A-Za-z\d!#$%&'*+-/=?^_`{|}~]
LOTS_OF_SYMBOLS

puts str.chomp

--output:--
[A-Za-z\d!#$%&'*+-/=?^_`{|}~]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top