CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
need help removing charactors from string

 



gerble1000
User

Jun 9, 2012, 6:44 AM

Post #1 of 14 (2573 views)
need help removing charactors from string Can't Post

how to remove every character from a string except these assci codes
48 to 57
63 to 90
97 to 122
32
39 to 41
44 to 46
127
8

how would i go about removing all other charactors from the string any ideas would be great :)


FishMonger
Veteran / Moderator

Jun 9, 2012, 8:29 AM

Post #2 of 14 (2572 views)
Re: [gerble1000] need help removing charactors from string [In reply to] Can't Post

What have you tried?

The most common approach would be to use a regex with a negated character class.


gerble1000
User

Jun 9, 2012, 8:53 AM

Post #3 of 14 (2569 views)
Re: [FishMonger] need help removing charactors from string [In reply to] Can't Post

i tried this

my $message = "Hello frank it's me graham ring me when u get 2 mins :)...";

$message =~ s/[^a-zA-Z0-9]*//g;
but this does not allow me to have commas and spaces and more.
i want to allow curtain charactors through


FishMonger
Veteran / Moderator

Jun 9, 2012, 9:04 AM

Post #4 of 14 (2568 views)
Re: [gerble1000] need help removing charactors from string [In reply to] Can't Post

Have you tried expanding the character class to include all of the desired characters?


gerble1000
User

Jun 9, 2012, 9:34 AM

Post #5 of 14 (2565 views)
Re: [FishMonger] need help removing charactors from string [In reply to] Can't Post

not quite sure how to do that as i borrowed this code from a google search


FishMonger
Veteran / Moderator

Jun 9, 2012, 9:51 AM

Post #6 of 14 (2563 views)
Re: [gerble1000] need help removing charactors from string [In reply to] Can't Post

Rather than simply copying something from the internet without knowing what it does, you should learn about regular expressions.

Start by reading the related perldoc's

http://perldoc.perl.org/perlrequick.html
http://perldoc.perl.org/perlretut.html
http://perldoc.perl.org/perlrecharclass.html

There are a couple others, but that will get you started.

It would also be a good idea to pick up a copy of Mastering Regular Expressions, 3rd Edition
http://shop.oreilly.com/product/9780596528126.do?green=17242849862&intcmp=af-mybuy-9780596528126.IP


gerble1000
User

Jun 10, 2012, 2:49 AM

Post #7 of 14 (2455 views)
Re: [FishMonger] need help removing charactors from string [In reply to] Can't Post

ok i have read the first link.
does this look ok
$message =~ s/[^a-zA-Z0-9\077\100\040\047\050\051\054\055\056]*//g;

\077\100
are octal codes for ? and @
i am assuming this will work.

i have learned that ^ finds everything except what follows.
~ matches and the * repeats till the end s will search and replace and the /g


gerble1000
User

Jun 10, 2012, 3:08 AM

Post #8 of 14 (2450 views)
Re: [gerble1000] need help removing charactors from string [In reply to] Can't Post

ok it seems to error out somewhere
this is the charactor causing all the problems



gerble1000
User

Jun 10, 2012, 4:24 AM

Post #9 of 14 (2440 views)
Re: [gerble1000] need help removing charactors from string [In reply to] Can't Post

ok i have it working now with this code
$message1 =~ s/[^a-zA-Z0-9\ \o{077}{100}{040}{047}{050}{051}{054}{055}{056}]*//g;
is this how you would have gone about doing it


FishMonger
Veteran / Moderator

Jun 10, 2012, 8:15 AM

Post #10 of 14 (2418 views)
Re: [gerble1000] need help removing charactors from string [In reply to] Can't Post

I'd start with this and adjust if needed.

$message =~ /[^-?@ ',.A-Za-z0-9]+//g;


gerble1000
User

Jun 11, 2012, 2:09 AM

Post #11 of 14 (2404 views)
Re: [FishMonger] need help removing charactors from string [In reply to] Can't Post

also on another note, how would you go about checking a file for a sentence with an if else statement. as this is what i have but only does a word and not a sentance.

open(FILE,"result.txt");
if (grep{/word/} <FILE>){
close FILE;
print "word found";
}
else{
close FILE;
print "not found";
}

instead of "word" i would like to have "word to look for"
i have tried this if (grep{/word\ to\ look\ for/}
but does not seem to work.
what am i doing wrong :)


BillKSmith
Veteran

Jun 13, 2012, 7:18 AM

Post #12 of 14 (2321 views)
Re: [gerble1000] need help removing charactors from string [In reply to] Can't Post

It does work for me. Perhaps you could post a sample of the data that it should match but does not. Is part of the phrase on one line and rest on another?
Good Luck,
Bill


gerble1000
User

Jun 16, 2012, 7:35 AM

Post #13 of 14 (1826 views)
Re: [BillKSmith] need help removing charactors from string [In reply to] Can't Post

your right that does work :)
but i can not do this


Code
if ((grep{/Message\ Sent\ Successfully/} <FILE>) || (grep{/Your\ message\ has\ been\ sent/} <FILE>)){ 
close FILE;
open(F, '>>:utf8', 'sentchecktest.txt');
print F "sent\n";
close F;
}

else{
close FILE;
open(F, '>>:utf8', 'sentchecktest.txt');
print F "error\n";
close F;
}


i would have used an elsif but it does not work on my version of perl "strange"


gerble1000
User

Jun 17, 2012, 7:00 AM

Post #14 of 14 (1784 views)
Re: [gerble1000] need help removing charactors from string [In reply to] Can't Post

sorted it
if (grep{/Message\ Sent\ Successfully/ || /Your\ message\ has\ been\ sent/} <FILE>){

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives