Gossamer Forum
Home : General : Perl Programming :

Reg Expression question

Quote Reply
Reg Expression question
I need some help with this one.
I'm using the below code, supplied by Chris071371, as part some form validation.
It's dsigned to catch long continious charactor strings from within a textarea.
Code:
@words = split(/\s/,$in{'Description'});
foreach (@words) {
print "$_<br>";
unless ($_ =~ /^\S{1,25}$/) {
push(@input_err, "$col (You have words that are too long!)");last;
}
}
Now I'm using \S, which should match a single non-whitespace character.
The problem I'm having is that of leading white spaces.
Trailing white spaces don't seem to be the problem, but even a single white space at the beginning of a single character string, will return an error.
All I need to do is to check all non-whitespace characters.

thanks
Bob

[This message has been edited by lanerj (edited January 26, 2000).]
Quote Reply
Re: Reg Expression question In reply to
I don't understand what you mean?

Please post more detail, including examples, of what you are trying to do.

--mark
Quote Reply
Re: Reg Expression question In reply to
Sorry, I'll try and explain this a bit better.
Ok, I'm working on a DBman project, and as part of a form, I have a textarea which is 50 characters in width, and 5 rows deep.
The search results are returned into a table which is only 30 characters in width, so If a user puts a continious line of characters into the textarea, It return that string as one line of 50 characters, distroying the table formating and the page.
So the above code is designed to catch words which are over 25 characters in length.
The origional code supplied by Chris used a period instead of \S, but which either one I use, It'm not catching leading white space characters.
Even one white space at the begining of the string will return an error.
I thought the \S was ment to match all non white space characters, but it dosn,t.
How can I ignore the leading white spaces.
I hope this makes better sense.
thanks
Bob


[This message has been edited by lanerj (edited January 26, 2000).]
Quote Reply
Re: Reg Expression question In reply to
Sorry Bob,

I didn't even think about leading white spaces. This will work, although I'm sure there is a better way.

Code:
$in{'Description'} =~ /^\s+(.+)$/ ? ($in{'Description'} = $1) : ($in{'Description'} = $in{'Description'});
@words = split(/\s/,$in{'Description'});
foreach (@words) {
unless ($_ =~ /^\S{1,25}$/) {
push(@input_err, "$col (You have words that are too long!)");
last;
}
}

------------------
Chris
Quote Reply
Re: Reg Expression question In reply to
Thanks heeps chris.
This problem has been giving me one major headace.
I'll try this out.
Thanks again.
Bob
Quote Reply
Re: Reg Expression question In reply to
Chris,
Almost works, It will chop the leading white spaces from the beginning of the string,
as in; (space)(space)word(space)word,
but there is still a problem with extra white spaces between words, as in; word(space)(space)word(space)(space)(space)word.
Dosn't like any extra spaces between words.

Thanks
Bob

[This message has been edited by lanerj (edited January 27, 2000).]
Quote Reply
Re: Reg Expression question In reply to
This will work for you Bob.

Code:
$in{'Description'} =~ s/\s+/ /g;
$in{'Description'} =~ /^\s+(.+)$/ ? ($in{'Description'} = $1) : ($in{'Description'} = $in{'Description'} );
@words = split(/\s/,$in{'Description'});
foreach (@words) { unless ($_ =~ /^\S{1,25}$/) {
push(@input_err, "$col (You have words that are too long!)");
last;
}
}

------------------
Chris
Quote Reply
Re: Reg Expression question In reply to
Yesssssss.That works.
Thanks Chris.

Bob
Quote Reply
Re: Reg Expression question In reply to
Still having a bit of of a problem with this.
Dbman converts all newline characters to `` when a record is added and converts them back again for printing.
The above code works fine for catching long words, but it also strips the new line characters as well.
Is there a way of keeping them in.
Thanks

Bob
Quote Reply
Re: Reg Expression question In reply to
I would suggest putting this before the code below:
Code:
$descr = $in{'description'};

And then, in the code itself, replace all the $in{'description'}'s with $descr. That way, if the validation succeeds, $in{'description'} is preserved.

------------------
Chris
Quote Reply
Re: Reg Expression question In reply to
what can I do in case I would like to use only the first 25 characters of a string?

Thanks.

Quote Reply
Re: Reg Expression question In reply to
$string =~ /^$string{1,25}/;


Paul Wilson.
http://www.wiredon.net/gt/
http://www.perlmad.com/
Quote Reply
Re: Reg Expression question In reply to
It doesn't work for me. Is there any other code I can use?