Gossamer Forum
Home : General : Perl Programming :

Stripping A Character

Quote Reply
Stripping A Character
I am working on a script where one or more of the fields in a form, all of which are required fields, contain a default value of just a single hypen. Ideally, the people completing the form would erase the hypen when entering the actual data. Things being as they are, though, I know that things are not always ideal.

I know how to check the field length to see if anything was entered in the field or to see if nothing was entered but the hyphen was erased (which should not be). My problem is when the person enters some text but fails to erase the hypen first.

Theoretically, the hypen could end up as the first character of the text or the last character of the text, depending on how they entered the rest of the text, or it may not be there at all. In addition, the entered text itself may contain a hyphen that needs to be there.

Question: how can I tell the script to check for a hyphen as the first or last character of text in a field and remove it if the field contains other data without removing any hyphen in any other position within the text?

Any help will be appreciated.
Quote Reply
Re: Stripping A Character In reply to
Bobsie,

I dont understand the problem exactly, but if you just want to know how to delete a leading or ending character. Here some help:

---
How do I strip blank space from the beginning/end of a string?

$string =~ s/^\s+//;
$string =~ s/\s+$//;
---

Replace \s with the code of a hypen.
Im dutch and dont know what a hypen is,
maybe you could tell me =)
Quote Reply
Re: Stripping A Character In reply to
 
Code:
$text =~
s/^ # Start of string
\-? # 0 or 1 dashes (must escape)
(.+) # 1 or more characters, save in $1
\-?$ # 0 or 1 trailing dash (must escape)
/$1/; # Remove the dash and replace with $1

Should work..

Cheers,

Alex

Quote Reply
Re: Stripping A Character In reply to
Alex,

It works, but only when the hyphen is at the beginning of the text. It looks like the ending hyphen is being picked up by the "(.+)" before the "\-?$" sees it, so it ends up being part of the text.

Is there anyway around that without just checking for standard alphanumeric characters in the text? Would I need to check for a match of a hyphen as the last character first and then, if found, do the substitution, and only do the first character check if the last character is not a hyphen? How would I do that?

chrishintz,

Thanks for trying to help. I already know how to do the substitution. The problem is, I will not know whether the hyphen (a "hyphen" is a "-" character, aka "dash" or "minus sign") is the first or last character of the text, or if there is another hyphen embedded in the text (which I do not want removed).
Quote Reply
Re: Stripping A Character In reply to
Oops.. That should be a

(.+?) # 1 or more characters, save in $1

The ? means non-greedy - match as little as possible. This way the -? will pick up the dash instead of the .+ grabbing it.

Also, I noticed you'll need a /x modifier on the end of the reg expression if you keep the comments in it.

Cheers,

Alex

[This message has been edited by Alex (edited May 17, 1999).]
Quote Reply
Re: Stripping A Character In reply to
Thanks Alex, that did the trick! I am not sure what you mean about the /x modifier, though, and there is no mention of it in the Perl book I have. The regexp seems to work identically with or without it though.

Thanks again!