+ Reply to Thread
Page 1 of 3 123 LastLast
Results 1 to 10 of 28

Thread: Regular Expressions

  1. #1
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Regular Expressions

    Delimiters
    Regular expressions must start and end with the same exact character. The two most common are below (although almost any non-alphanumeric character can be used).
    Code:
    /pattern/
    #pattern#
    Characters
    The most basic regular expression contains a single literal character. However using literals greatly restricts the capabilities of regular expressions. For that reason several special characters have been defined.
    Code:
    . (dot)  match any single character
    \d       match a digit 0-9
    \w       match an alphanumeric character
    \s       match a white space
    \D       match a non digit 
    \W       match a non aplhanumeric character
    \S       match a non space
    Character Classes
    Character classes are used to match one out of several characters. To use a character class, simply surround your characters by square brackets: [ and ]. You should note that unless otherwise specified, regular expressions are case sensitive (more on this later). Moreover a hyphen can be used to specify a range of characters.
    Code:
    [abc123]   match a, b, c, 1, 2 or 3
    [a-c1-3]   match a, b, c, 1, 2 or 3
    [a-z]      match a lower case letters
    [0-9]      same as \d
    [a-zA-Z]   match a lowercase or uppercase letter
    Quantifiers
    Often times you want to match a character or character class a certain amount of times. Quantifiers precede characters and character classes.
    Code:
    ?        match an item zero or one times
    *        match an item zero or more times
    +        match an item one or more times
    {n}      match an item n times
    {n,m}    match an item between n and m times
    {n,}     match an item n or more times
    Examples
    Below are several examples. As with any programming problem, there are always more than one way to do something. Each expression is placed on its own line and matches the pattern above.

    Pattern: 1234
    Code:
    /1234/       This only matches the pattern 1234
    /\d\d\d\d/   This matches any four digits
    /\d{4}/      This matches any four digits
    /[1-4]{4}/   This matches four digits that are between 1 and 4 inclusive
    Pattern: A domain name ending in .com
    Code:
    /[a-zA-Z0-9-]+.com/
    To see more examples, have a look at my validation class.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: Regular Expressions

    Great thanks a lot! This will be very helpful, and be a great reference for me.

    Thanks Bookmarked this, and will come back often for a reference.

    Thanks

  4. #3
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Regular Expressions

    I will also come back to this often for a reference. It's a lot easier than digging through my books lol

  5. #4
    Jordan Guest
    Nice reference resource. I will use this often.


    Posted via CodeCall Mobile

  6. #5
    Join Date
    Jan 2008
    Posts
    1,725
    Blog Entries
    4
    Rep Power
    29

    Re: Regular Expressions

    Nice reference, this will definitely help me instead of google searches lol. +rep / +bookmark!

  7. #6
    Jordan Guest

    Re: Regular Expressions

    John's posts on here have a way of appearing on Google in the top 10 anyway. We will probably see it there soon.

  8. #7
    Jordan Guest

    Re: Regular Expressions

    I believe we should turn this into a Cheat Sheet. I don't know about the rest of you, but I wouldn't mind having this hung up on the wall in my office for reference. What does everyone else think?

  9. #8
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Regular Expressions

    In fact, you should make it into a bedroom wallpaper, I'd put it up everywhere.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  10. #9
    Join Date
    Jan 2008
    Posts
    1,725
    Blog Entries
    4
    Rep Power
    29

    Re: Regular Expressions

    Quote Originally Posted by Jordan View Post
    I believe we should turn this into a Cheat Sheet. I don't know about the rest of you, but I wouldn't mind having this hung up on the wall in my office for reference. What does everyone else think?
    Agreed!

  11. #10
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Regular Expressions

    I've seen some RegEx cheatsheets before. I love cheatsheets
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread
Page 1 of 3 123 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Regular Expressions
    By chili5 in forum Java Tutorials
    Replies: 11
    Last Post: 06-14-2010, 04:13 PM
  2. Need help with regular expressions
    By Wiizle in forum PHP Development
    Replies: 5
    Last Post: 04-12-2010, 12:17 PM
  3. C# Regular Expressions
    By totonex in forum C# Programming
    Replies: 4
    Last Post: 12-12-2009, 11:11 AM
  4. regular expressions
    By Lop in forum C and C++
    Replies: 5
    Last Post: 09-16-2008, 08:38 AM

Tags for this Thread

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