data67 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, i have to see if i can parse a form entry (textbox) and depending on the input i would create a backdoor to another section of the application. Let me explain. I have textbox with a title of "Description" where people are required to provide some free form text. But if someone enters a e-mail address in this field it does something special. Now i was only searching for the "@" sign and the "." in the email string and that worked ok BUT this method has a flaw in it.
I need to set this reqular expression in such a way that :

(1) It will check if the "description" field has more that one word string (i.e., "admin@admin.address" NOT "my email is admin@admin.address"). If its the later then treat typed text as description.
(2) when it sees that there is only an email address in the "description" text box, only then would it use the typed in email address.

I know that this reqular expression is not very fancy but i need to somehow prove that this would work both in perl and in java.
Thanks for your help.

2004-05-12 Edit by jdporter: Changed title from 'special handling with reqular expressions'

  • Comment on special handling with regular expressions

Replies are listed 'Best First'.
Re: special handling with regular expressions
by Tomte (Priest) on May 10, 2004 at 14:43 UTC

    normaly you would use somthing like Mail::RFC822::Address to solve your problem; with a bit of escaping/translation effort the there used regexp is likely to work in java as it does in perl (assuming a j2sdk > 1.4.0); to help you translate it I suggest the new edition of the owl

    regards,
    tomte


    An intellectual is someone whose mind watches itself.
    -- Albert Camus

Re: special handling with regular expressions
by Abigail-II (Bishop) on May 10, 2004 at 14:45 UTC
    So, you are basically asking for a regular expression that recognizes email addresses. That's very hard. Email addresses are complex, and just accepting anything which has an @ and a dot, and no space will generate both false positives, and false negatives.

    There are several modules on CPAN that will validate email addresses. They have obvious names, a little search will reveal them. They don't work with a single regex though.

    Abigail

Re: special handling with regular expressions
by dave_the_m (Monsignor) on May 10, 2004 at 14:43 UTC
    if ($text =~ /^\s* \S+ \@ \w+ [\.\w]* \s*$/x) { .... }
        Yes, Merlyn. but i can't restrict myself to a any perl module b/c i know this reqular expression will be ported to java later on.
        I don't think the idea of handling form text value differently if it looks like an email addess is particularly wise, but if the OP is determined to go with such an approach, then my regex above is as good a heurstic as anything else. Sure, you could use a CPAN module that can correctly parse RFC822 addresses, but since we are dealing with a web form where users have typed in stuff by hand, its fairly unlikely they're typing the full RFCish stuff involving quoting, <> etc.
Re: special handling with regular expressions
by perlinux (Deacon) on May 10, 2004 at 14:46 UTC
    if ($description =~ /^\s*\w+@\w+\.\w+\s*$/) { $description =~ s/ //g; $email=$description; #it does something special.... } else { #something to do }
    This works,
    but an email address is very complex...is better to find a module