Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Formatting a long regex: can a character class [] be split overlines?

Reply
Thread Tools

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

 
 
Alexey Muranov
Guest
Posts: n/a
 
      05-01-2011
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.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Jose Calderon-Celis
Guest
Posts: n/a
 
      05-01-2011
[Note: parts of this message were removed to make it a legal post.]

http://es.w3support.net/index.php?db=so&id=150095

---
Jose Calderon-Celis





2011/5/1 Alexey Muranov <(E-Mail Removed)-toulouse.fr>

> 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.
>
> --
> Posted via http://www.ruby-forum.com/.
>
>


 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      05-02-2011
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/.

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      05-02-2011
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.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      05-02-2011
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!#$%&'*+-/=?^_`{|}~]

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 4.31 How can I split a [character] delimited string except when inside [character]? PerlFAQ Server Perl Misc 0 04-13-2011 10:00 PM
FAQ 4.31 How can I split a [character] delimited string except when inside [character]? PerlFAQ Server Perl Misc 0 01-25-2011 05:00 AM
Having compilation error: no match for call to (const __gnu_cxx::hash<long long int>) (const long long int&) veryhotsausage C++ 1 07-04-2008 05:41 PM
How can I split database results with ExecuteReader and Split? needin4mation@gmail.com ASP .Net 2 05-05-2006 10:36 PM
split() in String: asterisk in character class Michael Java 5 06-07-2004 01:49 PM



Advertisments