Jump to content

Regular Expressions

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
27 replies to this topic

#1
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
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
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
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

#3
John

John

    Writes binary right handed and hex left handed

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

#4
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Nice reference resource. I will use this often.


Posted via CodeCall Mobile

#5
morefood2001

morefood2001

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,720 posts
Nice reference, this will definitely help me instead of google searches lol. +rep / +bookmark!

#6
Guest_Jordan_*

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

#7
Guest_Jordan_*

Guest_Jordan_*
  • Guests
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?

#8
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
In fact, you should make it into a bedroom wallpaper, I'd put it up everywhere.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#9
morefood2001

morefood2001

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,720 posts

Jordan said:

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!

#10
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I've seen some RegEx cheatsheets before. I love cheatsheets :)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#11
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
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

#12
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
... and I'm gonna paste it all over me wall...
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums