match either whole alpha characters or whole numeric character but not alpha numeric characters

Posted on 2010-01-04
Last Modified: 2012-05-08

I need some help in regular expressions in perl

I am trying to get an regular expression for accepting the whole numeric value or a whole alphabets but not alpha numeric  values.

for example: it should accept "3445" or "apple" but not "34dsd"

Thanks in Advance

Question by:sudarshan1985
    LVL 2

    Accepted Solution

    LVL 13

    Expert Comment

    if("123123" =~ /^([a-zA-Z]+|[0-9]+)$/){
        print "ok\n";
    LVL 2

    Expert Comment

    This regex requires that a matching expression is either a sequence of alpha-numeric characters og numerals preceded and succeded by a word boundary symbol, e.g. a space, punctuation mark etc.


    Of course you would use the i-modifier in Perl to make it case insensitive.

    my $testcase = "afFfd5";

    if( $testcase =~ /\b[a-z]+\b|\b[0-9]+\b/i ) {
      print "Either numerals of characters exclusve\n";
    else {
      print "A mix of characters and numbers\n";
    LVL 6

    Expert Comment

    ronnyma's answer is complete. The author of the question probably need some guidance. He did not specify if the regex should be applied to a string or a text file.
    For string, try using this .... for a text file, ask for further help :-)
    use warnings;
    use strict;
    my $from_string = "123 gre45 apple 23 rum dr9";
    my @to_array = split /\s/, $from_string;
    foreach my $t ( @to_array )	{
    	if($t =~ m/\b[a-z]+\b|\b[0-9]+\b/)	{
    	print $t,"\n";
    # ======== out should be =============
    # ====================================
    I only copied ronnyma's regex and do not deserve any points. This is only to clarify things if the author of the question is a beginer.

    Open in new window


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    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…
    I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
    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…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    745 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

    18 Experts available now in Live!

    Get 1:1 Help Now