Article Diary - General Knowledge Digest

8 Basics of Regular Expressions

Regular expressions have been something that I was scared of when I started coding as a serious stuff. The string literals puzzled me like anything. preg_match, preg_grep, preg_split, preg_replace etc have been something I always wanted to avoid. This is for you guys who find regular expressions tacky.

1. “^tech

Searches a string that starts with tech.

2. “logy$”

Searches for strings that ends up in logy.

3. “a*b”

Looks for a string that has either zero or more a’s but exactly one b following a. (eg. b, ab, aab, aaab, aaaaaaaaaaaaaaaaab etc.).

4. “a+b”

Same as a*b but only difference is that atleast one a should be there in the string unlike a*b which can overlook a. (eg. ab, aab,aaaaaaaaab etc).

5. “a?b”

In this case string might have either zero or a single a (eg. ab or b) only these two possibilities are there.


To quantify the characters into certain sets and utilize them we can use paranthesis().

6. “(abc)+def”

Matches a string which consists of pattern having abc either one or n times followed by def at the end.


7. “(a|b)*c”

A string of a and b that ends in a c.

To specify range of characters. For instance to match that in a password small-case character, you can specify the set as [a-z] or [A-Z] or [0-9].

8. “([a-z])|([A-Z])”

Searches for a pattern having either small-case or capital letters.
5 (1)
Article Rating (1 Votes)
Rate this article
Attached Files
There are no attachments for this article.
There are no comments for this article. Be the first to post a comment.
Security Code Security Code
Related Articles RSS Feed
jCart - Free PHP AJAX Shopping Cart
Viewed 1529 times since Sun, Jan 3, 2010
New Tags Introduced in HTML5
Viewed 1547 times since Sat, Apr 3, 2010
Open Source WYSIWYG Editor
Viewed 1496 times since Thu, Jan 7, 2010
Tiny Flash MP3 Audio Player
Viewed 1163 times since Wed, Jan 6, 2010
Google Skipfish - Web Application Security Scanner
Viewed 1331 times since Mon, Mar 22, 2010
Free JavaScript Charts Library - JS Charts
Viewed 1222 times since Sun, Dec 13, 2009
Inline Form Validation With jQuery
Viewed 1539 times since Sat, Jan 9, 2010
50 Free And Quality Web Icons
Viewed 1851 times since Tue, Jan 5, 2010
How do I setup and use SSH?
Viewed 1833 times since Thu, Oct 15, 2009
ZeusCart | Feature Rich Open Source E-Commerce System
Viewed 1815 times since Thu, Jul 15, 2010