Regular Expressions

Posted on 2005-04-15
Last Modified: 2010-03-05
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.

this is something I would like to allow
$string = "Today is the day";
$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.
Question by:thoughtless
    LVL 1

    Accepted Solution


    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";
            print "ko\n";

    LVL 16

    Assisted Solution

    \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 ]*$/ ;}
    LVL 1

    Author Comment

    Thanks, I apperciate the help,

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
    A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now