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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need help with a regular expression

Status
Not open for further replies.

Leland123

Programmer
Aug 15, 2001
402
US
I'm having trouble creating a regular expression. I have an html form that contains the <textarea></textarea> tags. When the form is submitted, I capture the output of the <textarea> and write it to a .txt file. For some reason, unknown to me, carriage returns and whitespaces are mysteriously added to the output of the <textarea> on every post, even when the <textarea> is not edited, so the output of the textarea grows with each submission of the form.

I would like to strip the added carriage returns that always have varying number of whitespaces after them, so I need one regular expression that would match all of the following, where a whitespaces (eg \s) are represented by dots, and the carriage returns (eg \r) is represented as CR, for example:

here is a carriage return followed by four whitespaces:
$the_string="CR....";

Here is a carriage return followed by seven whitespaces.
$the_string="CR.......";

Here is a carriage return followed by eleven whitespaces.
$the_string="CR...........";

I've tried many regular expressions but nothing seems to work, unless I include a specific number of whitespaces in the regular expression like $string =~ s/\r\s\s\s\s/\r/g, but the string always begins with a CR followed by different numbers of whitespaces, depending on how many time the form has been submitted.

Here is the latest regular expression I've tried:

$the_string =~ s/(\r|\s+)\b/\r/g;

Here I was hoping to match, anywhere in the string, all CR(s) that are followed by an unknown number of whitespaces, and replace the matches with a single CR.

Any help would be greatly appreciated.

Regards,

LelandJ


Leland F. Jackson, CPA
Software - Master (TM)
Nothing Runs Like the Fox
 
actually.. try this

$var =~ tr/\x0D$/ /;
$var =~ s/\n/ /g;


That the code I have for getting rid of new lines in text area's.
 
why not try and determine the mysterious source of the carraige returns and white space. My guess is that it's the way you write to the file and nothing to do with the textarea tag or the form.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I can't remember why I was using x0D but I think it has something to do with the way newlines were translated in textarea's (as they allow new lines) and that was the only way I could get rid of them. I don't care about them anymore as I write everything out to DB's but back when I used flat files (circa 2003!) it drove me up the wall :)
 
Whoops, the original way I wrote the regular expression was working all along. I was just looking in the wrong place in the text file and seeing that no changes had occurred, when in fact the changes were being made in the place in the .txt file I was addressing in the program. I've been working on the app for too many hours. These regular expressions are working.

$FORM{ "enter_page" } =~ s/\r\s+/\r/g;
$FORM{ "html_title_tag" } =~ s/\r\s+/\r/g;
$FORM{ "sales_tax_tip" } =~ s/\r\s+/\r/g;
$FORM{ "ext_allowed" } =~ s/\r\s+/\r/g;
$FORM{ "docs_path" } =~ s/\r\s+//g;
$FORM{ "ext_allowed" } =~ s/\r\s+/\r/g;

I've also spent many hours today searching the web for some clue as to why the text in the <textarea> was creating carriage returns and spaces each time the form was submitted, but I didn't find a solution to that question. It could have something to do with the OS, since I'm running Linux. This problem only occurs with the text captured from the <textarea> of the html form when it is submitted. Regular textboxes, checkboxes, dropdown lists, etc do not have this problem, so I don't think it has to do with the way I'm written to the .txt file. The problem does also occur when the value stored in hidden fields is passed from the html form to the perl script. I resorted to using perl regular expression as a way of working around the problem. Here is the code:

Here I read the .txt file and store the values in it to variables which are displayed in a config html form.

#--------------------------

my %smconfig = ();

open (SMCONFIG, "kart_config") || die "The smconfig file is missing.\n";
while (<SMCONFIG>)
{
($tmpvar1, $tmpvar2) = split(/=/, $_);

$smconfig {$tmpvar1} = $tmpvar2;
}

close(SMCONFIG);

############ Uncomment for Debugging ############
#
# while(($key, $value) = each(%smconfig))
# {
# print qq~$key => $value<br>~;
# }
#
#################################################

$THE_COMPANY = $smconfig{SM_THE_COMPANY};
$THE_ADDRESS = $smconfig{SM_THE_ADDRESS};
$THE_CITY_ST_ZIP = $smconfig{SM_THE_CITY_ST_ZIP};
$THE_VERSION = $smconfig{SM_THE_VERSION};
$ENTER_PAGE = $smconfig{SM_ENTER_PAGE};
$HTML_TITLE_TAG = $smconfig{SM_HTML_TITLE_TAG};
$MAIN_PATH = $smconfig{SM_MAIN_PATH};
$PICTURE_PATH = $smconfig{SM_PICTURE_PATH};
$DOCS_PATH = $smconfig{SM_DOCS_PATH};
$SMTP_SERVER = $smconfig{SM_SMTP_SERVER};
$MAIL_FROM = $smconfig{SM_MAIL_FROM};
$EMAIL_TYPE = $smconfig{SM_EMAIL_TYPE};
$SHOW_EMAIL = $smconfig{SM_SHOW_EMAIL};
$THE_SEQ_HERE = $smconfig{SM_THE_SEQ_HERE};
$THE_WAIT_GET = $smconfig{SM_THE_WAIT_GET};
$ADM_SEQ_HERE = $smconfig{SM_ADM_SEQ_HERE};
$ADM_WAIT_GET = $smconfig{SM_ADM_WAIT_GET};
$THE_SERVER = $smconfig{SM_THE_SERVER};
$DNS = $smconfig{SM_DNS};
$THE_DBI = $smconfig{SM_THE_DBI};
$THE_HOST = $smconfig{SM_THE_HOST};
$THE_DB = $smconfig{SM_THE_DB};
$THE_PORT = $smconfig{SM_THE_PORT};
$THE_AUTOCOMMIT = $smconfig{SM_THE_AUTOCOMMIT};
$THE_OS = $smconfig{SM_THE_OS};
$BROWSE_ROWS = $smconfig{SM_BROWSE_ROWS};
$NO_OF_PAGES = $smconfig{SM_NO_OF_PAGES};
$LINKPOINT_DO = $smconfig{SM_LINKPOINT_DO};
$HOST_NAME = $smconfig{SM_HOST_NAME};
$PORT = $smconfig{SM_PORT};
$STORE_NUMBER = $smconfig{SM_STORE_NUMBER};
$KEY_FILE = $smconfig{SM_KEY_FILE};
$ORDERTYPE = $smconfig{SM_ORDERTYPE};
$ECHO_DO = $smconfig{SM_ECHO_DO};
$ECHO_SERVER = $smconfig{SM_ECHO_SERVER};
$ECHO_ORDER_TYPE = $smconfig{SM_ECHO_ORDER_TYPE};
$ECHO_TRANS_TYPE = $smconfig{SM_ECHO_TRANS_TYPE};
$ECHO_ID = $smconfig{SM_ECHO_ID};
$ECHO_PIN = $smconfig{SM_ECHO_PIN};
$ACCEPT_VISA = $smconfig{SM_ACCEPT_VISA};
$ACCEPT_MASTERCARD = $smconfig{SM_ACCEPT_MASTERCARD};
$ACCEPT_AMEX = $smconfig{SM_ACCEPT_AMEX};
$ACCEPT_DISCOVER = $smconfig{SM_ACCEPT_DISCOVER};
$ACCEPT_CHECK = $smconfig{SM_ACCEPT_CHECK};
$COOKIE_DIR = $smconfig{SM_COOKIE_DIR};
$COOKIE_NAME = $smconfig{SM_COOKIE_NAME};
$PERMANENT_COOKIE = $smconfig{SM_PERMANENT_COOKIE};
$SESSION_EXPIRE = $smconfig{SM_SESSION_EXPIRE};
$CHROOT = $smconfig{SM_CHROOT};
$TEST_FILE_SIZE = $smconfig{SM_TEST_FILE_SIZE};
$MAX_FILE_SIZE = $smconfig{SM_MAX_FILE_SIZE};
$TEST_DIR_SIZE = $smconfig{SM_TEST_DIR_SIZE};
$MAX_DIR_SIZE = $smconfig{SM_MAX_DIR_SIZE};
$OVERWRITE_FILES = $smconfig{SM_OVERWRITE_FILES};
$TEST_FILE_EXT = $smconfig{SM_TEST_FILE_EXT};
$EXT_ALLOWED = $smconfig{SM_EXT_ALLOWED};
$WIN_X = $smconfig{SM_WIN_X};
$WIN_Y = $smconfig{SM_WIN_Y};
$WIN_WIDTH = $smconfig{SM_WIN_WIDTH};
$WIN_HEIGHT = $smconfig{SM_WIN_HEIGHT};
$WIN_TOOLBAR = $smconfig{SM_WIN_TOOLBAR};
$WIN_LOCATION = $smconfig{SM_WIN_LOCATION};
$WIN_DIRECTORIES = $smconfig{SM_WIN_DIRECTORIES};
$WIN_STATUS = $smconfig{SM_WIN_STATUS};
$WIN_SCROLLBARS = $smconfig{SM_WIN_SCROLLBARS};
$WIN_RESIZABLE = $smconfig{SM_WIN_RESIZABLE};
$WIN_MENUBAR = $smconfig{SM_WIN_MENUBAR};
$PRINT_ON = $smconfig{SM_PRINT_ON};
$FORMATED_NO = $smconfig{SM_FORMATED_NO};
$THE_VALUE = $smconfig{SM_THE_VALUE};
$THE_VALUE_2 = $smconfig{SM_THE_VALUE_2};
$THE_CORD = $smconfig{SM_THE_CORD};
$THUMBNAIL_HEIGTH = $smconfig{SM_THUMBNAIL_HEIGTH};
$THUMBNAIL_WIDTH = $smconfig{SM_THUMBNAIL_WIDTH};
$DO_AFFILIATES = $smconfig{SM_DO_AFFILIATES};
$CONFIRM_UPDATES = $smconfig{SM_CONFIRM_UPDATES};
$CONFIRM_DELETES = $smconfig{SM_CONFIRM_DELETES};
$CONFIRM_DELETES = $smconfig{SM_CONFIRM_DELETES};
$IS_DEMO = $smconfig{SM_IS_DEMO};
$ACCOUNT_CC_INFO = $smconfig{SM_ACCOUNT_CC_INFO};
$DISPLAY_SOLD_ITEMS = $smconfig{SM_DISPLAY_SOLD_ITEMS};
$ONCLICK_MENU = $smconfig{SM_ONCLICK_MENU};
$SALT_KEY = $smconfig{SM_SALT_KEY};
$W3C_ICON = $smconfig{SM_W3C_ICON};
$SALES_TAX_TIP = $smconfig{SM_SALES_TAX_TIP};

#--------------------------------

The Admin can make change in the config form and when the config form is submitted, the setting are saved back to the .txt file until needed again:

#---------------------------

open (SMCONFIG, ">kart_config") or die ("I could not open the kart_config file.\n");


$FORM{ "the_version" } =~ s/\r/ /g;
$FORM{ "the_version" } =~ s/\s+$//;

#$FORM{ "enter_page" } =~ s/^\s+//; ## right trim string for whitespaces
#$FORM{ "enter_page" } =~ s/\s+$//; ## left trim string for whitespaces

$FORM{ "enter_page" } =~ s/\r\s+/\r/g;
$FORM{ "html_title_tag" } =~ s/\r\s+/\r/g;
$FORM{ "sales_tax_tip" } =~ s/\r\s+/\r/g;
$FORM{ "ext_allowed" } =~ s/\r\s+/\r/g;
$FORM{ "docs_path" } =~ s/\r\s+//g;
$FORM{ "ext_allowed" } =~ s/\r\s+/\r/g;


print SMCONFIG qq~SM_THE_COMPANY=$FORM{ "the_company" }
SM_THE_ADDRESS=$FORM{ "the_address" }
SM_THE_CITY_ST_ZIP=$FORM{ "the_city_st_zip" }
SM_THE_VERSION=$FORM{ "the_version" }
SM_ENTER_PAGE=$FORM{"enter_page"}
SM_HTML_TITLE_TAG=$FORM{ "html_title_tag" }
SM_MAIN_PATH=$FORM{ "main_path" }
SM_PICTURE_PATH=$FORM{ "picture_path" }
SM_DOCS_PATH=$FORM{ "docs_path" }
SM_SMTP_SERVER=$FORM{ "smtp_server" }
SM_MAIL_FROM=$FORM{ "mail_from" }
SM_EMAIL_TYPE=$FORM{ "email_type" }
SM_SHOW_EMAIL=$FORM{ "show_email" }
SM_THE_SEQ_HERE=$FORM{ "the_seq_here" }
SM_THE_WAIT_GET=$FORM{ "the_wait_get" }
SM_ADM_SEQ_HERE=$FORM{ "adm_seq_here" }
SM_ADM_WAIT_GET=$FORM{ "adm_wait_get" }
SM_THE_SERVER=$FORM{ "the_server" }
SM_DNS=$FORM{ "dns" }
SM_THE_DBI=$FORM{ "the_dbi" }
SM_THE_HOST=$FORM{ "the_host" }
SM_THE_DB=$FORM{ "the_db" }
SM_THE_PORT=$FORM{ "the_port" }
SM_THE_AUTOCOMMIT=$FORM{ "the_autocommit" }
SM_THE_OS=$FORM{ "the_os" }
SM_BROWSE_ROWS=$FORM{ "browse_rows" }
SM_NO_OF_PAGES=$FORM{ "no_of_pages" }
SM_LINKPOINT_DO=$FORM{ "linkpoint_do" }
SM_HOST_NAME=$FORM{ "host_name" }
SM_PORT=$FORM{ "port" }
SM_STORE_NUMBER=$FORM{ "store_number" }
SM_KEY_FILE=$FORM{ "key_file" }
SM_ORDERTYPE=$FORM{ "ordertype" }
SM_ECHO_DO=$FORM{ "echo_do" }
SM_ECHO_SERVER=$FORM{ "echo_server" }
SM_ECHO_ORDER_TYPE=$FORM{ "echo_order_type" }
SM_ECHO_TRANS_TYPE=$FORM{ "echo_trans_type" }
SM_ECHO_ID=$FORM{ "echo_id" }
SM_ECHO_PIN=$FORM{ "echo_pin" }
SM_ACCEPT_VISA=$FORM{ "accept_visa" }
SM_ACCEPT_MASTERCARD=$FORM{ "accept_mastercard" }
SM_ACCEPT_AMEX=$FORM{ "accept_amex" }
SM_ACCEPT_DISCOVER=$FORM{ "accept_discover" }
SM_ACCEPT_CHECK=$FORM{ "accept_check" }
SM_COOKIE_DIR=$FORM{ "cookie_dir" }
SM_COOKIE_NAME=$FORM{ "cookie_name" }
SM_PERMANENT_COOKIE=$FORM{ "permanent_cookie" }
SM_SESSION_EXPIRE=$FORM{ "session_expire" }
SM_CHROOT=$FORM{ "chroot" }
SM_TEST_FILE_SIZE=$FORM{ "test_file_size" }
SM_MAX_FILE_SIZE=$FORM{ "max_file_size" }
SM_TEST_DIR_SIZE=$FORM{ "test_dir_size" }
SM_MAX_DIR_SIZE=$FORM{ "max_dir_size" }
SM_OVERWRITE_FILES=$FORM{ "overwrite_files" }
SM_TEST_FILE_EXT=$FORM{ "test_file_ext" }
SM_EXT_ALLOWED=$FORM{ "ext_allowed" }
SM_WIN_X=$FORM{ "win_x" }
SM_WIN_Y=$FORM{ "win_y" }
SM_WIN_WIDTH=$FORM{ "win_width" }
SM_WIN_HEIGTH=$FORM{ "win_heigth" }
SM_WIN_TOOLBAR=$FORM{ "win_toolbar" }
SM_WIN_LOCATION=$FORM{ "win_location" }
SM_WIN_DIRECTORIES=$FORM{ "win_directories" }
SM_WIN_STATUS=$FORM{ "win_status" }
SM_WIN_SCROLLBARS=$FORM{ "win_scrollbars" }
SM_WIN_RESIZABLE=$FORM{ "win_resizable" }
SM_WIN_MENUBAR=$FORM{ "win_menubar" }
SM_THUMBNAIL_HEIGTH=$FORM{ "thumbnail_heigth" }
SM_THUMBNAIL_WIDTH=$FORM{ "thumbnail_width" }
SM_DO_AFFILIATES=$FORM{ "do_affiliates" }
SM_CONFIRM_UPDATES=$FORM{ "confirm_updates" }
SM_CONFIRM_DELETES=$FORM{ "confirm_deletes" }
SM_IS_DEMO=$FORM{ "is_demo" }
SM_ACCOUNT_CC_INFO=$FORM{ "account_cc_info" }
SM_DISPLAY_SOLD_ITEMS=$FORM{ "display_sold_items" }
SM_ONCLICK_MENU=$FORM{ "onclick_menu" }
SM_SALT_KEY=$FORM{ "salt_key" }
SM_W3C_ICON=$FORM{ "w3c_icon" }
SM_SALES_TAX_TIP=$FORM{ "sales_tax_tip" }\n;~;

close(SMCONFIG);

exit;

#--------------------------------

Regards,

LelandJ

Leland F. Jackson, CPA
Software - Master (TM)
Nothing Runs Like the Fox
 
if that works for you then all is well I suppose.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top