Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 7th, 2001, 08:40 AM
tpsinger tpsinger is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Posts: 3 tpsinger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Need a little help with regex

This may seem like a daft question but how can I alter a string so as to only leave letters and numbers, i.e. strip out all punctuation etc.

I currently use the following:

Code:
  $search_terms =~ s/\'//g;
  $search_terms =~ s/\"//g;
  $search_terms =~ s/\£//g;
  ...


as I don't know any better, can someone please help me out and show me something better.

TIA

Reply With Quote
  #2  
Old August 7th, 2001, 10:10 AM
Hero Zzyzzx's Avatar
Hero Zzyzzx Hero Zzyzzx is offline
11
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2001
Location: Lynn, MA
Posts: 4,635 Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 23 h 44 m 19 sec
Reputation Power: 77
Send a message via AIM to Hero Zzyzzx
Yeah, regexes are a pain to figure out.

A better way to think about them is what you want to leave in, and not what you want to take out. . .Otherwise you'd need to create a regex for every invalid character- a pain to say the least.

So,
Code:
$search_terms =~ s/[^\w\d\s]//g;


This takes anything that isn't whitespace, a digit or a valid perl identifier (which includes a-z _ and some other characters) and nixes it. This has the advantage of working with multiple languages if you "use locale;".

If you truly want to restrict it to only A-Z, 0-9 and whitespace, then use

Code:
# the a-zA-Z construct is faster than using the /i switch.
$search_terms =~ s/[^a-zA-Z\d\s]//g;


Look into "character classes" in regexes for this type of matching.

Reply With Quote
  #3  
Old August 7th, 2001, 10:22 AM
tpsinger tpsinger is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Posts: 3 tpsinger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
That's brilliant, thanks a lot.

I knew there must be an easier way to do it than taking out all the letters individually, just didn't know how to reverse it, cheers

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Need a little help with regex


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT