From wikipedia: “In theoretical computer science and formal language theory, a regular expression (abbreviated regex or regexp and sometimes called a rational expression[1][2]) is a sequence of characters that forms a search pattern, mainly for use in pattern matching with strings, or string matching, i.e. “find and replace”-like operations. The concept arose in the 1950s, when the American mathematician Stephen Kleene formalized the description of a regular language, and came into common use with the Unix text processing utilities ed, an editor, and grep (global regular expression print), a filter.”

Wonderful! But it’s only a little preview of the regEx world.

The possibilities are several. You can use a regular expression for find out some substring in your text, validate an user input or parsing a text.. The basic idea is simple: once you have created your pattern with a regular expression, you can use it to reach your aim.

The patterns have to be written using the regEx syntax, and this can be hard the first time, but when you learn how to write regular expressions, you will gain a lot of possibilities. This site is very useful: it offers to you an interactive tool to write and test all the regEx you want. Further, it shows popup when you move the cursor on the characters of you expression, giving to you an explanation of the highlighted char.

There are a lot of functions, for each programming language, for testing regEx. Usually, you have a function which takes in input a string and the regEx, and give to you a bool value.

Here I list some of the regular expression I’m using more in my job:

  • IP address: ([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}
  • email addresses, separate with ‘;’: ([A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4};)*
  • number of max 5 digits: \d{1,5}
  • alphanumerical string of max 10 chars: [0-9A-Za-z]{1,10}
  • telephon number (italian format): [+]*[0-9]{2,4}[/]*[0-9]{4,12}

In this other link you can found some slide from the Polytechnic of Turin (regEx from slide 23 to 82).

Advertisements