Wednesday, October 12, 2011

Regular Expressions

Today, we are going to see a common concept that is used in more situations, these are regular expressions. Regular Expressions are some patterns that describe an string. These are very useful for validations


E.g. to validate that an email is correct: email@domain.com (this is valid) / www.ccc.com (this is not valid).


We have a table with all features of regular expressions here:


Logical:
  • x|y: x or y.
  • xy: x followed by y

Character ranges:
  • [abc]: Any of characters in brackets. Ranges can be spefified, for example, [ad] is equivalent to [abcd])
  • [âbc]: Any character which is not in brackets.

Predefined character ranges:
  • .: Any single character, except the newline.
  • \d: Any digit character, equivalent to [0-9].
  • \D: Any character that is not digit, equivalent to [^0-9].
  • \s: Any single character in blank space (items, tabulations, break pages or newlines).
  • \S: Any single character that is not a blank space.
  • \w: Any alphanumeric character, including underscored, equivalent to [A-Za-z0-9_].
  • \W: Any character that is not alphanumeric, equivalent to [^A-Za-z0-9_].

Characters:
  • \f: Break pages.
  • \n: Newlines.
  • \r: Carriage return.
  • \t: Tabulation.

Limits:
  • ^: Beginning of input or line.
  • $: End of input or line.
  • \b: Limit of word (like an item or carriage return)
  • \B: End of word.

Quantifiers:
  • {n}: Exactly n appearances of the previous character.
  • {n,m}: At least n and at most m appearances of the previous character.
  • *:The previous character 0 or more times.
  • +: The previous character 1 or more times.
  • ?: The previous character at most 1. (that is, it indicate that the previous character is optional).



Source: http://luauf.com/2008/05/03/expresiones-regulares-en-java/

No comments:

Post a Comment