SitePoint Sponsor

User Tag List

Results 1 to 20 of 20
  1. #1
    SitePoint Addict Kokos's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Syntax of preg_replace

    Hey there guys, I have been looking for a GOOD tutorial on how to write the syntax for preg_replace(); preg_match; and that stuff. But I can't find anything...
    Somebody help me out :O

    Taking over the web one pixel at a time.
    Currently working @ CodeCreators

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, you want an introduction to regular expressions then. Here's a link for you:

    http://gnosis.cx/publish/programming...pressions.html

    this seems pretty nice, as it shows you visually what the expression is doing.

  3. #3
    SitePoint Addict Kokos's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well that looks nice indeed!
    I never understood what that codes did but thanks for the link!

    Taking over the web one pixel at a time.
    Currently working @ CodeCreators

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  5. #5
    SitePoint Addict Kokos's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, I was trying out some stuff but I couldn't figure out how to do this. I want someone to pick a username but it has to contain at least 1 normal character, 1 number and no spaces/special stuff. Also it has to be at least 6 characters long. I tried this:
    /([a-zA-Z]+[0-9]+|[0-9]+[a-zA-Z]+){6,}/
    (I cant figure out how to cancel special characters like $ so if your username contains that it rejects it..)

    Taking over the web one pixel at a time.
    Currently working @ CodeCreators

  6. #6
    SitePoint Member
    Join Date
    Apr 2004
    Location
    Est
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    /^[a-zA-Z-0-9]{6,}$/

    Also check out Regex Coach, it's freeware tool for testing regular expressions.

    http://weitz.de/files/regex-coach.exe

  7. #7
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    /^[a-z\d]{6,}$/i

    unfortunately, if you want to verify that there is at least one digit and one letter in the string you have to 2 more checks:

    check for at least one digit:
    /\d/

    check for at least one letter:
    /[a-z]/i

    i don't believe you can check for all 3 things in one expression since the php pcre functions don't allow for variable length look ahead/behind assertions.

    there's a very handy cheatsheet here

  8. #8
    SitePoint Addict Kokos's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what does the /i do?
    nvm found it on that cheatsheet
    but you cannot do those 3 things in one? Ok I could just check the length in another function. Thanks
    question, why does this: /[a-z0-9A-Z]/ match "asdf" but /^[a-z0-9A-Z]$/ doesn't?

    Taking over the web one pixel at a time.
    Currently working @ CodeCreators

  9. #9
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i is for "Case Insensitive".
    If you dont put i, then "me" is different from "Me" for the regular expression parser.
    ---------------------------
    Errors = Improved Programming.
    My Site

  10. #10
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by KoKoSNOOT
    what does the /i do?
    nvm found it on that cheatsheet
    but you cannot do those 3 things in one? Ok I could just check the length in another function. Thanks
    question, why does this: /[a-z0-9A-Z]/ match "asdf" but /^[a-z0-9A-Z]$/ doesn't?
    /[a-z0-9A-Z]/ = match any letter or digit
    /^[a-z0-9A-Z]$/ = match a string that is one char length and is a letter or a digit

    Since "asdf" is four chars length, the second expression won't match it, but the first one will because it contains a letter.
    Saul

  11. #11
    SitePoint Addict Kokos's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh ok, I thought ^ and $ meant the first and last word, not character. Thanks for pointing that out

    Taking over the web one pixel at a time.
    Currently working @ CodeCreators

  12. #12
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ^$ are reserver words in regular expression.

    ^ means start of the string you want to test
    $ means enf of the string you want to test.
    ---------------------------
    Errors = Improved Programming.
    My Site

  13. #13
    SitePoint Addict Kokos's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, by the way I had another question, I created this pattern to test if something was a correct e-mail address. What is wrong with it?
    /[\w\d]\@[\w\d]\.[\w\d]/
    What I think the above code is supposed to check, if a string contains this:
    Any character or digit, then @, then any character or digit, then ., then any character or digit.

    Taking over the web one pixel at a time.
    Currently working @ CodeCreators

  14. #14
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i am not sure as i dint read this, but you can read this thread http://www.sitepoint.com/forums/show...=email+pattern

    it has nice pattern for all possible email formats.
    ---------------------------
    Errors = Improved Programming.
    My Site

  15. #15
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by KoKoSNOOT
    Ok, by the way I had another question, I created this pattern to test if something was a correct e-mail address. What is wrong with it?
    /[\w\d]\@[\w\d]\.[\w\d]/
    What I think the above code is supposed to check, if a string contains this:
    Any character or digit, then @, then any character or digit, then ., then any character or digit.
    Nothing's wrong, I did a quick test -- it worked. Also you can remove \d as it's redundant here. \w means a letter, or a digit, or an underscore.
    Saul

  16. #16
    Chessplayer kleineme's Avatar
    Join Date
    Apr 2004
    Location
    Germany
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    even if you shorten it to
    /\w@\w\.\w/
    then it will match any string with ONE word character, followed by "@", followed by ONE word character, followed by a ".", follwed by ONE more word character, e.g. "a@b.c". I won't call this a secure check for a valid mail address You would at least have to add some "+":
    /\w+@\w+\.[a-zA-Z]{2,4}/
    Never ascribe to malice,
    that which can be explained by incompetence.
    Your code should not look unmaintainable, just be that way.

  17. #17
    SitePoint Addict Kokos's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks
    hmm for some reason the above pattern also accepts email@email.comcomcom
    so {2,4} doesn't do anything ...
    edit: nvm the above

    Taking over the web one pixel at a time.
    Currently working @ CodeCreators

  18. #18
    Chessplayer kleineme's Avatar
    Join Date
    Apr 2004
    Location
    Germany
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi again,

    right, try /^\w+@\w+\.[a-zA-Z]{2,4}$/
    Never ascribe to malice,
    that which can be explained by incompetence.
    Your code should not look unmaintainable, just be that way.

  19. #19
    SitePoint Addict Kokos's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ya, I figured
    btw this is my final pattern:
    /^\w{4,}@\w{3,}\.[a-z]{2,4}$/i

    Taking over the web one pixel at a time.
    Currently working @ CodeCreators

  20. #20
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Speaking as someone who first touched regular expressions about 4 years ago and am STILL fine-tuning my prowress at writing them, I can offer a few comments/tips.

    1) Regular expressions are EXTREMELY explicit. They do exactly what you tell them to. So, a good policy to adopt when writing them is to be, well, anal (maybe vigilant is a more friendly word :P). If a pattern isn't working right it's because a) the syntax doesn't support what you're trying to do or b) you screwed up. Guess which one is more likely?

    2) Learn the terminology. Regular expression syntax has it's own little vernacular that when fully understood can greatly aid any future conversations you have on the topic. Get to know metacharacters, quantifiers, sub-expressions, assertions (positive, negative, look-ahead, look-behind), full-stop, backreferences, matching, modifiers (or flags), etc.

    3) Do a search on these forums (or just the web) and dissect patterns you find into their smallest parts (often referred to as "atoms"). This will help you understand what they do and how they're put together.

    4) Stick with it. Learning what regular expressions are for is easy. Reaching decent proficiency with them takes a while. Mastering them is a long journey.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





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
  •