Hi Phillip,
This is similar to the uppercase problem: why raise an error when you
can just do it yourself?
That’s precisely what I’m trying to do, i.e strip leading/trailing
spaces silently. I’ve already realized my code is in error by
referring to ‘parameters’ rather than ‘params’.
I’m fine with the RE stuff. In case you’re interested, I’ll explain
what the code does and provide a small Ruby program to demo it.
If you’re not interested about Ruby regex’s, READ NO FURTHER and
accept my thanks for your willingness to look ino my problem. BTW, I
just posted a question about my “params” stumbling block.
Best wishes,
Richard
The expression /^\s*([A-Za-z0-9])+\s*$/ says:
^ Start scanning at the beginning
\s match any “whitespace” character, e.g. space, tab, etc.
\s* match zero or as many as possible (with some caveats) whitespace
characters
() match the specification within the parentheses and assign $1 a
reference to the matching value
[A-Za-z0-9] matches any of the characters A to Z, a to z and 0 to 9,
i.e. any of these 62 characters
A-Za-z0-9]+ matches one or more of the such characters, as many as
possible, with some caveats
So when params[:symbol] (containing the data the user supplied in the
textbox labelled Symbol) matches this pattern, then $1 contains the
string of legitimate character surrounded front and back by zero or
more whitespace characters. If for example, the params[:symbol]
contained " AB CD ", this string would NOT match the pattern, so $1
would me nil and my algorithm would leave params unchanged, and the
subsequent constraint would flag the entry as invalid.
But if the parms[:symbol] contained " ABCD ", that string would be
replaced with “ABCD” and the subsequent constraint would be
satisfied.
Here’s a Ruby example:
DATA.each { |line|
params = Hash.new
params[:symbol] = line.chomp
params[:symbol] =~ /^\s*([A-Za-z0-9]+)\s*$/
params[:symbol] = $1 if $1
printf(“"%s" matches %s
params[:symbol] = "%s"\n”, line.chomp,
($1 ? $1 : ‘nil’),
params[:symbol])
}
END
ABCD
DE CD
DEXCD
On Jun 24, 5:56 pm, Phillip K. [email protected]