Regular expressions to find the presence of special characters

Discussion in 'Perl Misc' started by Vertica Garg, Oct 25, 2004.

  1. Vertica Garg

    Vertica Garg Guest

    I am trying to validate that the string contains characters [A-Z a-z 0-9 /
    .. -] only. If there is any other charcter in the string, I have to print the
    error message. No special characters like *, &, $ etc should be allowed.

    Example:
    $code=Hello*Hi

    $code=567$fg

    $code=@ghyt

    For each of the above, I want to print error message.

    Is there any easy way of doing it using regular expressions?

    Regards,
    Vertica
    Vertica Garg, Oct 25, 2004
    #1
    1. Advertising

  2. Vertica Garg wrote:
    > I am trying to validate that the string contains characters [A-Z a-z
    > 0-9 / . -] only. If there is any other charcter in the string, I have
    > to print the error message. No special characters like *, &, $ etc
    > should be allowed.


    Ask the negated question: You got an error condition if your string contain
    any character _not_ in that class.

    [^A-Z a-z0-9 / . -]

    Unfortunately the doc page entry is well hidden way down in "perldoc
    perlre":

    You can specify a character class, by enclosing a list of characters in
    "[]", which will match any one character from the list. If the first
    character after the "[" is "^", the class matches any character not in
    the list.

    jue
    Jürgen Exner, Oct 25, 2004
    #2
    1. Advertising

  3. Vertica Garg

    Vertica Garg Guest

    Thanks Jue..

    But unfortunately, this does not work in all the cases.
    e.g., if

    $code="@KK.L";
    if ($code=~m/[^A-Za-z0-9.]/)
    {
    print "Found";
    }

    I expected the program to print "Found" but it does not.

    Regards,
    Vertica


    "Jürgen Exner" <> wrote in message
    news:pH4fd.1139$dW.528@trnddc08...
    > Vertica Garg wrote:
    > > I am trying to validate that the string contains characters [A-Z a-z
    > > 0-9 / . -] only. If there is any other charcter in the string, I have
    > > to print the error message. No special characters like *, &, $ etc
    > > should be allowed.

    >
    > Ask the negated question: You got an error condition if your string

    contain
    > any character _not_ in that class.
    >
    > [^A-Z a-z0-9 / . -]
    >
    > Unfortunately the doc page entry is well hidden way down in "perldoc
    > perlre":
    >
    > You can specify a character class, by enclosing a list of characters

    in
    > "[]", which will match any one character from the list. If the first
    > character after the "[" is "^", the class matches any character not in
    > the list.
    >
    > jue
    >
    >
    Vertica Garg, Oct 25, 2004
    #3
  4. "Vertica Garg" <> writes:
    > But unfortunately, this does not work in all the cases.
    > e.g., if
    >
    > $code="@KK.L";
    > if ($code=~m/[^A-Za-z0-9.]/)
    > {
    > print "Found";
    > }
    >
    > I expected the program to print "Found" but it does not.


    This may be platform-dependent, but on my machine, the same thing
    happens. You need to output a newline as well. This will work:

    $code="@KK.L";
    if ($code=~m/[^A-Za-z0-9.]/)
    {
    print "Found\n";
    }
    Arndt Jonasson, Oct 25, 2004
    #4
  5. Vertica Garg wrote:
    > Thanks Jue..
    >
    > But unfortunately, this does not work in all the cases.
    > e.g., if
    >
    > $code="@KK.L";
    > if ($code=~m/[^A-Za-z0-9.]/)
    > {
    > print "Found";
    > }
    >
    > I expected the program to print "Found" but it does not.


    Enable strictures and warnings to have Perl help you prevent such mistakes!

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Oct 25, 2004
    #5
  6. Vertica Garg

    Anno Siegel Guest

    Arndt Jonasson <> wrote in comp.lang.perl.misc:
    >
    > "Vertica Garg" <> writes:
    > > But unfortunately, this does not work in all the cases.
    > > e.g., if
    > >
    > > $code="@KK.L";
    > > if ($code=~m/[^A-Za-z0-9.]/)
    > > {
    > > print "Found";
    > > }
    > >
    > > I expected the program to print "Found" but it does not.

    >
    > This may be platform-dependent, but on my machine, the same thing
    > happens. You need to output a newline as well. This will work:
    >
    > $code="@KK.L";
    > if ($code=~m/[^A-Za-z0-9.]/)
    > {
    > print "Found\n";
    > }


    Without "strinct" and "warnings", Perl will silently interpolate the
    (empty) package variable "@LL", so the result is ".L". That doesn't
    match. Try '@LL.L' instead.

    Anno
    Anno Siegel, Oct 25, 2004
    #6
  7. [Please do not top post!]
    [Tried to rearrange text into chronological sequence]
    [Please do not blindly fullquote but trim the quotation to the amount needed
    to provide context]
    Vertica Garg wrote:
    > "Jürgen Exner" <> wrote in message
    >> Vertica Garg wrote:
    >>> I am trying to validate that the string contains characters [A-Z a-z
    >>> 0-9 / . -] only. If there is any other charcter in the string, I
    >>> have to print the error message. No special characters like *, &, $
    >>> etc should be allowed.

    >>
    >> Ask the negated question: You got an error condition if your string
    >> contain any character _not_ in that class.
    >>
    >> [^A-Z a-z0-9 / . -]

    >
    > But unfortunately, this does not work in all the cases.
    > e.g., if
    >
    > $code="@KK.L";


    What does the array @KK contain?

    Or did you mean
    $code='@KK.L';
    or
    $code="\@KK.L";

    jue
    Jürgen Exner, Oct 25, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Douglas
    Replies:
    0
    Views:
    594
    Jay Douglas
    Aug 15, 2003
  2. Stefan Mueller
    Replies:
    3
    Views:
    32,960
    Stefan Mueller
    Jul 23, 2006
  3. Alan
    Replies:
    1
    Views:
    361
  4. Robert Dodier

    seek/tell in presence of multibyte characters

    Robert Dodier, Nov 24, 2006, in forum: Perl Misc
    Replies:
    7
    Views:
    140
    Brian McCauley
    Nov 26, 2006
  5. Noman Shapiro
    Replies:
    0
    Views:
    222
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page