Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: regular expression to check if the textbox contains only special characters

by toolic (Bishop)
on Oct 17, 2016 at 13:09 UTC ( [id://1174148]=note: print w/replies, xml ) Need Help??


in reply to regular expression to check if the textbox contains only special characters

Just check for at least one upper-case letter:
use warnings; use strict; while (<DATA>) { if (/[A-Z]/) { print 'good '; } else { print 'bad '; } print; } __DATA__ * *,* *.* ABC ABC,DEF,GHI_JKL

Outputs:

bad * bad *,* bad *.* good ABC good ABC,DEF,GHI_JKL

Replies are listed 'Best First'.
Re^2: regular expression to check if the textbox contains only special characters
by choroba (Cardinal) on Oct 17, 2016 at 14:55 UTC
    I'm not sure whether the OP wants *.*,*;A to pass. I'd use
    chomp; print /[^A-Z_,]/ ? 'bad' : 'good'

    Or, if the format is important, too:

    print /^(?:[A-Z_]+,?)+(?<!,)$/ ? 'good' : 'bad';

    i.e. at least one uppercase letter or underscore, followed by a comma, this repeating several times, and no trailing comma.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
       print /^(?:[A-Z_]+,?)+(?<!,)$/ ? 'good' : 'bad';

      Hi I tried your suggestion as below

      while (<DATA>) { if (/^(?:[A-Z_]+,?)+(?<!,)$/) {print "good";} else {print "bad";} print; }
      __DATA__ * *,* *.* ABC ABC* *ABC ABC_TO ABC_TO,DEF,GHI_JKL,GHI_JK1_TO

      Good string format are ABC,ABC_TO and ABC_TO,DEF,GHI_JKL,GHI_JK1_TO but somehow I am getting this as BAD ABC_TO,DEF,GHI_JKL,GHI_JK1_TO

        The digit 1 doesn't belong between A and Z:

        ABC_TO,DEF,GHI_JKL,GHI_JK1_TO ~

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
        Hi choroba

        Understood this and solved the situation for me.

         print /^(?:[A-Z_]+,?)+(?<!,)$/ ? 'good' : 'bad';

        should be modified as

         print /^(?:[A-Z_0-9]+,?)+(?<!,)$/ ? 'good' : 'bad';

        Lookahead and Lookbehind concepts is intresting. Thank you very much.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1174148]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-18 13:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found