Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12

Thread: simple regex

  1. #1
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts

    simple regex

    i have this code

    if (preg_match("{[a-zA-z]}", $variable) && !preg_match("{[0-9]}", $variable)) {

    what i want is that variable can only contain letters, and that works fine. but, how can i combine that reg ex into just one.

    And one more thing. I would also like to disallow characters like ! " # $... etc.

    Do i need to create something like {[!"#$%&/()=?*]} and disallow it, or is there a special way to do that.

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts

  • Users who have thanked kbluhm for this post:

    matak (11-02-2007)

  • #3
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    with character groupings, you can negate the matching, so to check if a string contains nothing but a-z, case-insensitively, you can do
    PHP Code:
    $pattern '#[^a-z]#i';
    if(
    preg_match($pattern$string)>0) {
      
    //there was something other than a letter

    Alternatively, ditch the regexp and use the ctype_alpha() function, that given a string returns false if there are any non-alphabetic characters in it.
    http://uk2.php.net/ctype-alpha
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • Users who have thanked GJay for this post:

    matak (11-02-2007)

  • #4
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Thanks for the ctype info, but i would like to learn regex.
    @GJay -> your regex isn't working right..

    i tried this if (preg_match("{#[^a-z]#i}", $variable)) { and it's never true
    i checked out a bit my regex literature and i couldn't find # anywhere. btw, i like to use perl regex not posix if that means anything in this case.

    thanks anyway...

  • #5
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    The hash symbol (#) is simply a delimiter.

    In PHP's patterns, you always start with a delimiter (the first character in the pattern) and end with the same. Then follow it with any modifiers. These all work the same:

    Code:
    /[a-z]/i
    #[a-z]#i
    %[a-z]%i
    It's whatever you find easier to read.

    Rather than solve this relatively simple problem, I'll give a design tip.

    Instead of using this:

    PHP Code:
    if (preg_match("{#[^a-z]#i}"$variable)) { 
    Why not create this:

    PHP Code:
    function checkAlpha($value) {
        return 
    preg_match("{#[^a-z]#i}"$value);

    Now you have a nifty little function that solves your problem. And you won't have to rewrite it anywhere. If you put it in a "library" file, you can re-use it for other sites.

  • Users who have thanked aedrin for this post:

    matak (11-02-2007)

  • #6
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    thanks for the design tip, but obviusly this does nothing..
    if (preg_match("{#[^a-z]#i}", $variable)) {
    i know that ^ means begins with, and i means case insensitive, but that match doesn't match anything. why can't someone just write working example. is it my karma or what?
    Last edited by matak; 11-02-2007 at 04:28 PM.

  • #7
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    It's the "{}" you put into the regex string. preg_* functions require you to put the regex petween two same characters (a delimiter), so it can distinguish between the actual regular expression and the flags you set (i in you case).

    Apparently "{}" can also be used as a pair of delimiters (as you would get an error if there would be no closing delimiter), which means that the function will attempt to match the following:
    The # character,
    Any character that is not a lower-case letter,
    The # character
    The 'i' character.
    Using: "#[^a-z]#i" will only match any character which is not a lower-case letter.
    Last edited by marek_mar; 11-02-2007 at 04:38 PM.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • Users who have thanked marek_mar for this post:

    matak (11-02-2007)

  • #8
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    i used to much delimiters. i thought that even 'i' needs to be inside delimiters. i was wrong. Thanks GJay for providing solution in first post, and sorry for me not seeing the obvious.
    tnx marek for undeleting the last post i think i'm ready for next regex step (validating email :lol) so stay tuned it's gonna be a lota fun...

  • #9
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by marek_mar View Post
    Using: "#[^a-z]#i" will only match any character which is not a lower-case letter.
    A HA!!

    this has totaly different meaning!

    #^[a-z]#i than this #[^a-z]#i

    in first case it matches the first characters!! in second it means everything but the characters inside!!!

    i'm going to buy few bananas couse i feel like monkey :-(l)

  • #10
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Quote Originally Posted by matak View Post
    but obviusly this does nothing..
    if (preg_match("{#[^a-z]#i}", $variable)) {
    Correct. I was merely quoting your regular expression.

  • #11
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    quoting or mocking

    and here's for the e-mail

    {{[a-z0-9]}i@{[a-z0-9]}i.{[a-z]}i{3}

    i really wonder will this work at all

  • #12
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Figured it out. This is for now what i'm gonna use, just i have one question..
    here's regy "{[a-z]@[a-z][^.][a-z]{3-4}}" - well that is not so complicated, but i was wondering on why do i need [^.]. Why didn't it work with something this simple "{[a-z]@[a-z]\.[a-z]{3-4}}" or even "{[a-z]@[a-z][\.][a-z]{3-4}}" . for don't know what reason i used [^.], and it works now. i'll try to figure that one out by myself, but sure could use your help.


  •  

    Posting Permissions

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