What is a regular expression?

Asked

Viewed 665 times

24

In many codes I see functions with a set of special characters that seem to have a function of their own.

I see it in virtually every language, so what is it after all, a class, a library or what then ?

Although there is an answer in that question, in my view there may be a confusion of concepts when it is stated:

regex are deterministic finite automaton

That is, this answer does not answer this question in its basic concept.

A deterministic finite automaton is a finite state machine that in turn are recognizers of formal language types being one of them regular expressions and it is on this last that I look for definitions and characteristics in its implementations.

2 answers

16

Is a string that defines a search pattern. It is an algorithm. We can say that it is a language for finding patterns in text.

She’s not a programming language, but its implementation is available in virtually all standard libraries of programming languages, or by third party library.

Some implementations are quite simple having only one function, others have several classes with several utilities. Some languages prefer to embed it as a construct. There are language variations. Some more complete, others only with the main codes.

We can say it’s a design pattern, but how it is so old, so pervasive, that no one even defines in this way.

It is a way of obtaining the localization of text patterns within other texts through a sequence of codes that indicate how this pattern should be composed to fit what is sought. Other ways can be tedious, require larger code and the programmer can make mistakes more easily, although inexperienced programmers can create several false positives if they do not test properly.

It is common to have functions, which in addition to locating makes the change of a pattern by some other text.

It is usually abbreviated as regex or regexp.

I find it particularly unreadable and prefer functions that indicate more "fully" what you want (some languages have libraries for this) and it is often much slower than making a loop in the hand (some cases can be quite complicated, but if you have functions that help is not bad). I would respect it more if it were a variation of ABNF.

  • 3

    I think what he’s looking for is an explanation of how finite automata are interpreted as [0-9] becomes a state that accepts only numbers.

  • I have been looking in the OS about regular expressions to give as reference in another question and only found this question/answer with the definition, the answer is consistent but I think it lacks examples.

2

A regular expression is simply a set of characters that make a search pattern, for example, instead of writing the full name of a file, you can write the beginning of the name followed by the character '*', so it will reference all items with this pattern.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.