Results 1 to 5 of 5

Thread: Regular expressions - matching two possible patterns using | (pipe)

  1. #1
    Join Date
    Jun 2008
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Regular expressions - matching two possible patterns using | (pipe)

    Hi guys,

    Have been trawling the web for quite some time with no luck - hopefully somebody can help!

    I have a regular expression that checks for a suitably formatted email address, and I have a seperate process for catching blanks. Therefore I need a regex that accepts either a valid email address or no data at all.

    Here's my existing regex...

    PHP Code:
    if(!ereg('^[0-9A-Za-z.-_]+@[0-9A-Za-z]+\.[A-Za-z.]+$',$value)) $badFormat 1
    From web searching I gather that I can use the | (pipe) character to seperate two arguments in a regex, but I'm unclear on the syntax, this is what I tried...

    PHP Code:
    if(!ereg('^[0-9A-Za-z.-_]+@[0-9A-Za-z]+\.[A-Za-z.]+$'|'^[A-Z]{4}$',$value)) $badFormat 1
    ... but it didn't work - as didn't countless other variations!

    Can somebody help me?

    Thanks in advance!

    Chris.

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Code:
    <?php
    $test='abc@domain.info';
    if(preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$|^[Þ]{4}$/', 
            $test)) {echo "YES";}
    else {echo "NO";}
    ?>
    You could probably manipulate the above code to suit your needs. The Þ symbol is just that; a symbol. You said you wanted to match either email addresses or no data at all. I wasn't sure how to do no data at all, so I just added a random symbol to match if the data is not an email.

    This example uses preg_match as opposed to ereg as I am more familiar with preg stuff and it is less processor heavy.

    Lemme know if you have any other questions.
    To choose the lesser of two evils is still to choose evil. My personal site

  3. #3
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    try:
    PHP Code:
    if(!ereg('^([0-9A-Za-z.-_]+@[0-9A-Za-z]+\.[A-Za-z.]+|)$',$value)) $badFormat 1
    so it matches [0-9A-Za-z.-_]+@[0-9A-Za-z]+\.[A-Za-z.]+ or blank
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@§†ê® §©®¡þ† /\/\@|{ê®
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    That's lovely, but disallows some perfectly valid email addresses (foo+bar@baz(quux)@my-site.co.uk, for example). The full specification of a valid email address is very complicated, and it's generally not worth your time to validate it beyond a very basic check:
    Code:
    if (!empty($value) && !preg_match('/^.+@.+\..+$/', $value))
        $badFormat = true;
    Note: preg_match() is preferred to ereg() in almost all cases. It's faster and more powerful.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Jun 2008
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Thanks for your replies guys - have seen the light and decided to get my head into Perl regular expressions rather than POSIX so I can take advantage of the preg_match() functions!

    Have read in quite a few places that the old ereg (POSIX) style functions are being removed in a future version (6?) the speed of Perl regex is far better.

    Chris.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •