Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

Regular Expressions

Was wondering if someone could provide me with a regular expression that would search for any nonword character in a string but ignore white spaces.

Example:
this is something I would like to allow
$string = "Today is the day";
or
$string = "Today is the 2nd day";

This is something I would like to catch
$string = "T*day is the day";

maybe I have been staring at this thing to long as it seems like a simple thing to do, but for some reason I can't seem to make a regex to work.
0
thoughtless
Asked:
thoughtless
2 Solutions
 
pio0203Commented:
Hi,

A possible solution is:

use strict;
use warnings;

use strict;
use warnings;


my @strings = ( "Today is the 2nd day", "T*day is the day") ;

foreach (@strings)
{
    if (/^[\w\d\s]+$/)
    {
        print "ok\n";
    }
    else
    {
        print "ko\n";
    }
}


0
 
manav_mathurCommented:
\w also matches '_' (underscore). If you don;t want that

my @strings = ( "Today is the 2nd day", "T*day is the day") ;
foreach (@strings){print "Found nonmatching" unless /^[a-zA-Z0-9 ]*$/ ;}
0
 
thoughtlessAuthor Commented:
Thanks, I apperciate the help,
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now