Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Regular Expressions

case sensitive

  • Please log in to reply
27 replies to this topic

#1 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 03 September 2008 - 03:29 PM

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).
/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.
. (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.
[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.
?        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 [I]n[/I] times
{n,m}    match an item between [I]n[/I] and [I]m[/I] times
{n,}     match an item [I]n[/I] 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
/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
/[a-zA-Z0-9-]+.com/

To see more examples, have a look at my validation class.
  • 2

#2 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 03 September 2008 - 04:16 PM

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

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

Thanks :D
  • 0

#3 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 03 September 2008 - 04:27 PM

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

#4 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 03 September 2008 - 04:32 PM

Nice reference resource. I will use this often.


Posted via CodeCall Mobile
  • 0

#5 morefood2001

morefood2001

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1011 posts

Posted 03 September 2008 - 06:01 PM

Nice reference, this will definitely help me instead of google searches lol. +rep / +bookmark!
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#6 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 04 September 2008 - 03:47 AM

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

#7 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 06 September 2008 - 07:43 AM

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?
  • 0

#8 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 06 September 2008 - 08:37 AM

In fact, you should make it into a bedroom wallpaper, I'd put it up everywhere.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#9 morefood2001

morefood2001

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1011 posts

Posted 06 September 2008 - 10:31 AM

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!
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#10 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 06 September 2008 - 11:49 AM

I've seen some RegEx cheatsheets before. I love cheatsheets :)
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#11 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 06 September 2008 - 01:26 PM

Cheatsheets are great. :D I wish I had more of them for Java, and PHP. I'm gonna print this tutorial as a reference. :D
  • 0

#12 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 07 September 2008 - 09:24 AM

... and I'm gonna paste it all over me wall...
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?





Also tagged with one or more of these keywords: case sensitive

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download