Re: multi-line Strings
On 12/15/2012 6:54 AM, Chris Uppal wrote:
Arne Vajh?j wrote:
The regex syntax itself is not exactly a good example of readability.
This, I think, is the point. We don't need a special String syntax to fix the
problem with regexps -- we'd be much better off with a fixed regexp syntax.
Something OO. And (since this is Java) I don't think that we need be afraid of
something verbose.
Off-the-top-of-my-head (all classes and method are imaginary):
Regexp alpha = Regexp.fromList(java.lang.text.portable.Alphas);
alpha = alpha.or('_');
Regexp num = Regexp.fromList(java.lang.text.portable.Digits);
Regexp alphanum = alpha.or(num);
Regexp identifier = alpha.followedBy(alphanum.repeated());
Naturally, I'd prefer something a /bit/ less verbose, but Java won't support
that. But even with the verbosity, I think my version is /far/ better. It
puts the composition of regexps into the programmer's hands which means that it
can be approached like any other complex programming task. Quoting/escaping
problems go away. Grouping (bracketing) problems go away (and become
decoupled from the backreference concept. Comments become trivially easy to
add. Various kinds of abstraction and reuse are possible.
I would assume that it has been tried many times to come up with
a nicer syntax for regex. Tried without success.
It is not a problem for the simplex regex, but the complex ones
are tricky.
P.S. Mind you: my /real/ opinion is that regular expressions have no place in
production code except in the construction of scanners (for which a more
directly-applicable implementation than standalone regexps is helpful).
Regexps are for users to enter, or go into configuration data. At least the
suggestion above has the advantage -- from my point of view -- that regexps no
longer look like "quick and easy" fixes to problems, and maybe the programmer
would think more about whether they /actually/ solve [all of] the problem at
hand.
Regex is widely used for general data validations. From form input
validation in web apps to XML schema definitions.
Arne