What is the meaning of compiling a regular expression? Not only in java but also in other languages like python has compiled representation of a regular expression. Actually it always bothers me what happens when a regular expression is compiled? All I know is it makes further matching of patterns faster. But I wonder how?

Regular expressions are the same for all programming languages, the difference is in the syntax of the programming language.

Java example
http://www.javamex.com/tutorials/regular_expressions/

The same regex in Python

>>> import re
>>> nine = "123456789"
>>> ten = "0123456789"
>>> p = re.compile(".*[0-9]{10}.*")
>>> c = p.match(nine)
>>> print c
None
>>> d = p.match(ten)
>>> print d
<_sre.SRE_Match object at 0x011B7AA0>
>>> d.group()
'0123456789'

Learn about Regular Expression Syntax and then you can implement into all programming langugaes... :)

Learn about Regular Expression Syntax and then you can implement into all programming langugaes... :)

Regular expression syntax isn't standardized, different languages may have their own regex syntax, a subset of another language's syntax, or a regex package that mostly implements another language's syntax but adds features or has different syntax for certain things. But regex syntax is often similar in completely different languages.

Obviously you have missed the point of my post. I know about regular expression and their workings in java, ruby, perl and python. But the question was that what does compile do? In 'dive into python' the author first discusses about the regular expression and then the overhead of regular expression then he discusses about calling the method compile and claims that it makes the whole thing faster. But I am not sure what compile does. I always wondered whether they do some kind of preprocessing like building a suffix tree or something like that. But this seemed mysterious and I could never appreciate the significance of the method compile.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.