Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #17068
| Message-ID | <3150175.r8Iz2dskB7@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2012-11-07 23:21 +0100 |
| Subject | Re: Good use of bitwise NOT "~" or unnecessary obfuscation? |
| Newsgroups | comp.lang.javascript |
| References | (6 earlier) <timstreater-72DE22.22494904112012@news.individual.net> <d725e235-3ad0-4e29-939b-d59d5f02beb3@y8g2000yqy.googlegroups.com> <48mi98trj215lkcssp9hq412ddggfnijed@4ax.com> <168bd0ef-ba1a-4448-ba80-8dac4e4c26ad@g8g2000yqp.googlegroups.com> <a7GdnW4cw5bI7QTNnZ2dnUVZ_ridnZ2d@earthlink.com> |
| Followup-To | comp.lang.javascript |
Followups directed to: comp.lang.javascript
Patricia Shanahan wrote:
> Scott Sauyet wrote:
>> I personally can't see it. My example was intentionally verbose and
>> as explicit as I could comfortably be, so as to remove the regex
>> terseness factor from the equation and see if Tim still had
>> objections. The objections many have to Perl are similar to one main
>> objection to regexes: they are often simply unreadable: write-only
>> code. Your abbreviations are shortened enough to no longer be clear.
>> None of the keywords except "OR" are really obvious. It really
>> doesn't look much clearer to me than `/^\s+|\s+$/g `.
>
> I think there is a significant difference between Perl and regex. In
> Perl, terseness is a choice. One can use meaningful identifiers, white
> space, and comments. I have written maintainable Perl code.
>
> In regex, there is no option for meaningful identifiers, or, as Gene
> pointed out, white space. Comments have to be either before or after the
> entire regex, not interleaved with it.
Apples and oranges. Perl is a *programming language*; regular expressions
are part of a *technique* (pattern matching) usable *in* programming
languages.
And apparently you are not aware that *especially* Perl's implementation of
regular expressions, and Perl-Compatible Regular Expressions (PCRE) as
supported by e.g. PHP, allow what you are asking for with the `x'
(PCRE_EXTENDED) flag:
my $leadingOrTrailingWhitespace =
/
^ # (start of input
# followed by
\s+ # at least one whitespace)
| # or
\s+ # (at least one whitespace
# followed by
$ # end of input)
/x;
In PHP (with PCRE):
<?php
$leadingOrTrailingWhitespace =
'/
^ # (start of input
# followed by
\s+ # at least one whitespace)
| # or
\s+ # (at least one whitespace
# followed by
$ # end of input)
/x';
$s = preg_replace($leadingOrTrailingWhitespace, '', ' foo ');
?>
In ECMAScript implementations (with JSX:regexp.js [1], revisions 272 and
later):
var leadingOrTrailingWhitespace = new jsx.regexp.RegExp(
[
"^ # (start of input",
" # followed by",
"\\s+ # at least one whitespace)",
"| # or",
"\\s+ # (at least one whitespace",
" # followed by",
"$ # end of input)"
].join("\n"),
"x"
);
or (standards-compliant since Ed. 5, proprietarily available before):
var leadingOrTrailingWhitespace = new jsx.regexp.RegExp(
"^ # (start of input \n\
# followed by \n\
\\s+ # at least one whitespace) \n\
| # or \n\
\\s+ # (at least one whitespace \n\
# followed by \n\
$ # end of input",
"x"
);
> I would be much happier if one could build regular expressions up in
> pieces, using identifiers to reference components:
>
> leadingSpaces = /^\s+/
> trailingSpaces = /\s+$/
> leadingOrTrailingSpaces = /{leadingSpaces} | {trailingSpaces}/
>
> s = s.replace(/{leadingOrTrailingSpaces}/g, "");
Built-in (ECMA-262-5.1, ยง15.10.4):
var leadingSpaces = "^\\s+";
var trailingSpaces = "\\s+$";
var leadingOrTrailingSpaces =
new RegExp(leadingSpaces + "|" + trailingSpaces, "g");
var s = s.replace(leadingOrTrailingSpaces, "");
or
var leadingSpaces = "^\\s+";
var trailingSpaces = "\\s+$";
var leadingOrTrailingSpaces =
new RegExp([leadingSpaces, trailingSpaces].join("|"), "g");
var s = s.replace(leadingOrTrailingSpaces, "");
(You can find more examples in JSX:regexp.js where I am building the
expressions to implement, for example, Unicode character property classes.)
With JSX:regexp.js (revisions 275 and later):
var leadingSpaces = /^\s+/;
var trailingSpaces = /\s+$/;
var leadingOrTrailingSpaces =
jsx.regexp.concat(leadingSpaces, "|", trailingSpaces);
s = s.replace(new jsx.regexp.RegExp(leadingOrTrailingSpaces, "g"), "");
or
var leadingSpaces = /^\s+/;
var trailingSpaces = /\s+$/;
var leadingOrTrailingSpaces = leadingSpaces.concat("|", trailingSpaces);
s = s.replace(new jsx.regexp.RegExp(leadingOrTrailingSpaces, "g"), "");
I am considering
var leadingSpaces = /^\s+/;
var trailingSpaces = /\s+$/;
var leadingOrTrailingSpaces =
jsx.regexp.alternate(leadingSpaces, trailingSpaces);
s = s.replace(new jsx.regexp.RegExp(leadingOrTrailingSpaces, "g"), "");
and
var leadingSpaces = /^\s+/;
var trailingSpaces = /\s+$/;
var leadingOrTrailingSpaces = leadingSpaces.alternate(trailingSpaces);
s = s.replace(new jsx.regexp.RegExp(leadingOrTrailingSpaces, "g"), "");
What do you think?
PointedEars
___________
[1]
<http://PointedEars.de/websvn/filedetails.php?repname=JSX&path=%2Ftrunk%2Fregexp.js>
<http://PointedEars.de/scripts/test/regexp>
--
Sometimes, what you learn is wrong. If those wrong ideas are close to the
root of the knowledge tree you build on a particular subject, pruning the
bad branches can sometimes cause the whole tree to collapse.
-- Mike Duffy in cljs, <news:Xns9FB6521286DB8invalidcom@94.75.214.39>
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Good use of bitwise NOT "~" or unnecessary obfuscation? RobG <rgqld@iinet.net.au> - 2012-11-01 18:32 -0700
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Denis McMahon <denismfmcmahon@gmail.com> - 2012-11-02 06:14 +0000
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Patricia Shanahan <pats@acm.org> - 2012-11-02 02:28 -0700
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-11-02 10:22 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Tim Streater <timstreater@greenbee.net> - 2012-11-02 10:37 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Patricia Shanahan <pats@acm.org> - 2012-11-02 06:25 -0700
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Asen Bozhilov <asen.bozhilov@gmail.com> - 2012-11-02 06:35 -0700
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2012-11-02 14:58 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Patricia Shanahan <pats@acm.org> - 2012-11-02 08:49 -0700
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2012-11-04 21:52 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Tim Streater <timstreater@greenbee.net> - 2012-11-04 22:49 +0000
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2012-11-05 13:18 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Scott Sauyet <scott.sauyet@gmail.com> - 2012-11-06 06:48 -0800
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Tim Streater <timstreater@greenbee.net> - 2012-11-06 15:04 +0000
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Gene Wirchenko <genew@ocis.net> - 2012-11-06 10:43 -0800
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Scott Sauyet <scott.sauyet@gmail.com> - 2012-11-06 11:22 -0800
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Patricia Shanahan <pats@acm.org> - 2012-11-06 12:16 -0800
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Scott Sauyet <scott.sauyet@gmail.com> - 2012-11-06 12:53 -0800
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2012-11-07 17:34 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-07 23:21 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-11-07 23:23 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Gene Wirchenko <genew@ocis.net> - 2012-11-07 17:00 -0800
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-11-08 07:16 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Dr J R Stockton <reply1245@merlyn.demon.co.uk.invalid> - 2012-11-07 17:50 +0000
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Dr J R Stockton <reply1245@merlyn.demon.co.uk.invalid> - 2012-11-05 19:26 +0000
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Stefan Weiss <krewecherl@gmail.com> - 2012-11-07 22:13 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Gene Wirchenko <genew@ocis.net> - 2012-11-02 09:51 -0700
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-02 18:41 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Patricia Shanahan <pats@acm.org> - 2012-11-02 12:30 -0700
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Tim Streater <timstreater@greenbee.net> - 2012-11-02 20:33 +0000
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2012-11-04 21:57 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Tim Streater <timstreater@greenbee.net> - 2012-11-04 22:44 +0000
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Andrew Poulos <ap_prog@hotmail.com> - 2012-11-05 15:41 +1100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Tim Streater <timstreater@greenbee.net> - 2012-11-05 09:07 +0000
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-02 22:10 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Patricia Shanahan <pats@acm.org> - 2012-11-02 15:22 -0700
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-03 10:19 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Patricia Shanahan <pats@acm.org> - 2012-11-03 13:05 -0700
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-03 21:39 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-03 12:26 +0100
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2012-11-02 12:28 +0200
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Gene Wirchenko <genew@ocis.net> - 2012-11-02 10:00 -0700
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Asen Bozhilov <asen.bozhilov@gmail.com> - 2012-11-02 06:15 -0700
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Scott Sauyet <scott.sauyet@gmail.com> - 2012-11-06 06:32 -0800
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? RobG <rgqld@iinet.net.au> - 2012-11-06 18:23 -0800
Re: Good use of bitwise NOT "~" or unnecessary obfuscation? Scott Sauyet <scott.sauyet@gmail.com> - 2012-11-07 11:22 -0800
csiph-web