Regex

Regular Expression

A sequence of characters that define a search pattern. These search patterns are used to match and manipulate strings, or sets of strings.

The concept of regular expressions was first introduced in the 1950s when the American mathematician Stephen Kleene formalized the description of regular languages, which are a class of languages that can be recognized by finite automata. In the 1980s, regular expressions were introduced to the UNIX world, and since then, they have become a standard feature in many programming languages and tools.

Almost all modern programming languages support regular expressions. Some examples include Python, Java, C++, C#, and JavaScript. In addition, many text editors, such as vim, emacs, and Sublime Text, support regular expressions for search and replace operations.

Regular expressions can be very powerful and can include a wide range of special characters and syntax to match and manipulate strings. There are many online resources and tutorials available that can help you learn more about regular expressions and how to use them.

Here’s an example of a regular expression that can validate an international phone number:

^\+(?:[0-9] ?){6,14}[0-9]$

Here’s a breakdown of each of the steps in the above regular expression:

  1. ^ – This symbol matches the beginning of the string.
  2. \+ – This matches the plus symbol at the beginning of the phone number. The backslash is used to escape the special meaning of the plus symbol, which is to match one or more occurrences of the preceding character.
  3. (?:[0-9] ?){6,14} – This is a non-capturing group that matches between 6 and 14 occurrences of a digit (0-9) followed by an optional space. The ?: is used to create a non-capturing group, which means that the group will be matched, but it will not capture the text matched by the group. Capturing groups are used to store the text matched by a portion of the regular expression for later use.
  4. [0-9] – This matches a single digit (0-9).
  5. $ – This symbol matches the end of the string.

Here are some examples of phone numbers that would match this regular expression:

  • +1 555 555 5555
  • +44 20 7123 4567
  • +61 2 9876 5432
  • +1 (555) 555-5555
  • +44 20 7123 4567

And here are some examples of phone numbers that would not match:

  • 555-555-5555 (missing leading “plus” symbol)
  • +1 555 555 (too few digits)
  • +1 555 555 55555 (too many digits)

Keep in mind that this is just one way to validate an international phone number, and there are many other regular expressions that could be used for this purpose. It is also important to note that this regular expression will not verify that the phone number is actually in use or that it belongs to a specific individual. It will only verify that the number is in a valid format.

Also abbreviated regexp.

  • Abbreviation: Regex