Regex to find all non-alphanumeric except dash (-)

Posted on 2006-05-28
Last Modified: 2009-12-16
I'm useless at regular expressions and can't for the life of me find what I need to enter as my pattern in preg_match_all to find all non-alphanumeric characters except dashes (-). Alphanumeric characters can be upper or lower case but no spaces or anything else aside from dashes can be allowed.

For anyone who has their head wrapped around regex I hope this is an easy one! Many thanks.
Question by:wjdashwood
    LVL 5

    Accepted Solution


    if (preg_match('/[^a-zA-Z0-9\-]/', $string))
        die('Invalid string!');


    Author Comment

    Thank you aminerd, now how come I just can't get my head around creating that by myself?!?  :)

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    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

    22 Experts available now in Live!

    Get 1:1 Help Now