Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stripping characters from text?

Status
Not open for further replies.
Apr 13, 2006
9
GB
hey

This is the code I found online which strips spaces from the end of a text field in my form before its saved to the flatfile

$strip = $form{'gametitle'};
$strip =~ s/\s*$//;

How do i change that so it strips out other characters like these " ? > @ $ & ..and so on?

 
Leave the existing lines, and add a
Code:
$strip =~ s/[?@$&*;]//g;
Just add more characters in between the [...]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Some of those metacharacters have special meanings to the re engine. In particular, if you wish to include a "-" or a "^" character, it should be the first character or it needs escaping with a "\". If you wish to include a period or a backslash it would also need escaping.

Yours,

fish

["]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.["]
--Maur
 
periods/dots do not need escaping inside a character class, the dash needs to be escaped unless it's placed all the way to the right in the character class, but if a person is unsure they can just escape any and all characters to be safe.
 
According to perlre in the section "Version 8 Regular Expressions"
Finally, the ``.'' metacharacter matches any character except ``\n'' (unless you use /s).

Am I missing something?

f


["]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.["]
--Maur
 
the dot has no meta value inside a character class:

Code:
my $foo = 'foo.bar';
my $bar = 'foobar';
for($foo,$bar) {
   print "Dot you! [$_]$/" if (/[.]/);
}

Same with some other meta characters when inside a character class. You just didn't read down far enough in the tutorial:

We saw in the section above that there were ordinary characters, which represented themselves, and special characters, which needed a backslash \ to represent themselves. The same is true in a character class, but the sets of ordinary and special characters inside a character class are different than those outside a character class. The special characters for a character class are -]\^$

There is even an example in the tutorial:

Code:
    /end\./;          # matches 'end.'
    /end[.]/;         # same thing, matches 'end.'

although I realize that's not totally clear that the dot in the character class is just not matching any character after the word end. It's actually matching a literal dot as the above code I posted shows.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top