Tuesday, June 30, 2009

Regular expressions quick list

One of the most essential things you need to know as a software engineer or as a software engineering student is regular expressions. This post will brief you about the basics of regular expressions, again remember this is just a layman's view and not a geeks perspective, so read this and move on.....

Metacharacters -------------------------Matches
  1. . -------------------------------Anything except newline character
  2. \n -----------------------------Newline
  3. * ------------------------------Zero or more copies of preceding expression
  4. + -----------------------------One or more occurrence of preceding expression
  5. ? ------------------------------Zero or one copies of preceding expression
  6. ^ -----------------------------Beginning of a line
  7. $ ------------------------------End of line
  8. a|b ---------------------------a or b
  9. (ab)+ -----------------------One or more copies of ab (grouping)
  10. "a+b" -----------------------Literal "a+b"
  11. [] ------------------------------Character class
Examples :-
  1. abc------------------------------------- abc
  2. abc* -----------------------------------ab abc abcc abcc abccc ......
  3. abc+ ----------------------------------abc abcc abccc.....
  4. a(bc)+ -------------------------------abc abcbc abcbcbc....
  5. a(bc)? --------------------------------a abc
  6. [abc] ----------------------------------one of:a,b,c
  7. [a-z] -----------------------------------any letter, a-z
  8. [a\-z] ----------------------------------one of : a,-,z
  9. [-az] ---------------------------------- one of : - , a, z
  10. [a-zA-Z0-9]+ ----------------------one or more alphanumeric characters
  11. [ \t\n]+ -------------------------------whitespace
  12. [^ab] ---------------------------------anything except : a,b
  13. [a^b] ---------------------------------one of : a, ^, b
  14. [a|b] ----------------------------------one of : a, | , b
  15. a|b ------------------------------------one of : a, b
  16. ^[a-z] --------------------------------line starting with a character from a-z
  17. [a-z]$ --------------------------------line ending with a character from a-z
  18. ^[a-z]$ -----------------------------line containing only a character from a-z
Thanking you,
Layman

No comments:

Post a Comment