Quantcast
Post Pic

8 basics of regular expression that can make you expert

Well, 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. But thanks to phpbuilder and php that I can now handle them with some ease. And this is for you guys who find regular expressions tacky.

.

VERY BASICS:

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.

.

QUANTIFYING THE CHARACTERS:

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.

.

LOGICAL OPERATORS [OR(|) & AND(.)]:

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.

.

That’s all
Cheers!!

Delicious Bookmark


Comments

03.08.10

Why wouldn’t you combine 8 as [a-zA-Z] ?

03.08.10

Short but sweet post, thanks

03.08.10

This is all very basic stuff. Check out perldoc regex tutorial for the real advanced deal. It’s well written and easy enough to understand so you won’t be lost anymore with regex – perldoc.perl.org

03.08.10

I love the post! It would be great if you could show example bit’s of code for illustration purposes though….

03.08.10

thanks. and i would welcome some good ways to work aroundand tackle them. Still learning and healthy criticism is always welcome.. :)

well steve, you always learn new things when ever you criticized on it. well this helps you about to improve and prove the best.

03.08.10

First i have to say i love this, this almost solved a particular problem i have been having. Would it be possible to take it a step further. I am using this on a dynamically created table with collapsable sections.

03.08.10

Well, regular expressions have been something that I was scared of when I started coding.

03.08.10

the best expression always was and always is to be natural!!!

men’s briefcases

Leave Your Response

* Name, Email, Comment are Required