Latest web development tutorials

Regular Expressions - Introduction

Unless you've used regular expressions, or you may not be familiar with this term. However, there is no doubt that you have already used some of the regular expression does not involve the concept of the script.

For example, you are likely to use? And * wildcards to find files on your hard disk. ? Wildcard matches a single character in the file name, and the * wildcard matches zero or more characters. ? Such as data .dat model will look for the following files:

data1.dat
data2.dat
datax.dat
dataN.dat

* Character instead of the? Character to expand the number of files found. data * .dat Match all of the following documents:

data.dat
data1.dat
data2.dat
data12.dat
datax.dat
dataXYZ.dat

Although this search method is useful, but it is still limited. By understanding the * wildcard works, it introduces the regular expression depends concept, but regular expressions more powerful, and more flexible.

Regular Expressions can be obtained by a simple approach to achieve powerful. The following first give a simple example:

 ^.+@.+\\..+$ 

Continue reading this tutorial will give you free to use this code.

Why use regular expressions?

A typical search and replace operations require that you provide the exact text of the search results with the expected match. Although this technique to perform a simple search and replace static text on the task may have been enough, but it lacks flexibility, the use of this method to search for dynamic text, if not impossible, at least will become very difficult.

By using regular expressions, you can:

  • Test pattern within a string.
    For example, you can test the input string to string within the view mode whether the phone number or credit card number patterns. This is known as data verification.
  • Replacement text.
    You can use regular expressions to identify specific text in the document, completely remove the text or replace it with other text.
  • Based on pattern matching to extract a substring from a string.
    You can find within text documents or enter a specific domain.

For example, you may need to search the whole site, remove outdated material, as well as replace some HTML formatting tags. In this case, you can use regular expressions to determine whether the material or the HTML formatting tags in each file. This process will narrow the list of affected files to those files you want to delete or change the material. You can then use regular expressions to remove obsolete material. Finally, you can use regular expressions to search and replace tags.

Development History

Regular expression "ancestor" can always be traced back to the early study of the human nervous system works. Warren McCulloch and Walter Pitts two neurophysiologists developed a mathematical way to describe these neural networks.

In 1956, a man named Stephen Kleene mathematician on the basis of McCulloch and Pitts earlier work, published an article titled "Neural network event representation," the paper introduces the concept of regular expressions. Regular expressions are used to describe the expression of what he called "a regular set of algebra", so a "regular expression" this term.

Subsequently, this work can be found to apply to the use of some earlier studies Ken Thompson computational search algorithm, Ken Thompson is the principal inventor of Unix. The first practical application of regular expressions is in the Unix qed editor.

As they say, the rest is known history. From then until now based on regular expressions are an important part of editors and search tools in the text.

Applications

Currently, the regular expression has been widely used in many software, including * nix (Linux, Unix, etc.), HP and other operating systems, PHP, C #, Java and other development environments, and many application software, you can see shadow regular expressions.

C # regex

In our tutorial C #, C # regular expressions in this chapter devoted to the knowledge about the C # regular expressions.

Java regular expressions

In our Java Tutorial, Java regular expression of this chapter devoted to the relevant Java regular expression of knowledge.

JavaScript regular expressions

In our JavaScript tutorial, JavaScript RegExp objects of this chapter devoted to the knowledge of JavaScript regular expressions, we also provide a complete JavaScript RegExp Object Reference Manual .

Python Regular Expressions

In our base Python tutorial, Python regular expressions in this chapter devoted to the knowledge of Python regular expressions.

Ruby Regular Expressions

In our tutorial Ruby, Ruby regular expressions in this chapter devoted to the knowledge of the Ruby regular expressions.