"
Supported by

Have you used regular expressions before?

Regularly expressions have been around for ages. But they’re not very well known and even less used. They are a sort of descriptive language that allows for making research and very sophisticated replacements in alpha-numeric chains.

A simple example: to validate an email address. If you have to do this through an algorithm you’ll have to work hard. However, with regular expressions, only two or three code lines and the regular expression “\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b” are enough.

It’s almost always necessary to research and to make relatively complex replacements within alpha-numeric chains, and it’s almost certain that resorting to regular expressions is preferable to developing your own algorithm.

There are two steps in using regular expressions.

  1. Creating the ABAP code to do what we want, either a research, a validation, or a replacement;
  2. Determining the regular expression per se that solves our problem.

Considering the first step there are two possible approaches to use regular expressions in ABAP:

  • Use the FIND and REPLACE commands, together with REGEX;
  • Use CL_ABAP_REGEX and CL_ABAP_MATCHER classes.

The first one is more simple and direct, but the second one is much more flexible and has the advantage of better performance if we intend to apply the thing on a mass level.

Here you’ll find the online official SAP documentation on regular expressions in ABAP. And here you’ll find a PDF teaching how to use both methods and which is a great reference to learn how to use regular expressions in ABAP. All over the web, there are examples and documentation on how to use regular expressions in ABAP.

Concerning the second step, it’s important to keep in mind that:

  • Regular expressions are a pattern, available in most programming languages, and as such, the same regular expression should have a similar behaviour in ABAP, Java or any other language implementing it. Thus, in order to learn regular expressions, you won’t have to do it in the ABAP context;
  • You are not inventing the wheel. By being so powerful, regular expressions are complex. On most typical circumstances there’s already a regular expression created by someone which will do exactly what you need. Thus, before you waste time trying to make your own regular expression, search for one that’s already been made. There are regular expressions databases online, such as this;
  • Here you have a good tutorial on regular expressions. It’s based on .NET, but that’s not relevant, since, as I’ve said, it’s all the same;
  • The fabulous program DEMO_REGEX_TOY (SE38) allows you to test your regular expression and adjust before you finally place it on the program where you intend to use it.

I seldom use regular expressions. But when I do, I know the alternative would be painful. Above all, what you need to know is that they exist, their scope and how they work, in order to realise the advantage of using them.

If you’re curious, Wikipedia is a good starting point to learn about this fascinating tool.

Thank you, Irene Nobrega for the photo.

Greetings from Abapinho.

5 comentários a “Have you used regular expressions before?”

  1. RSousa Diz:

    Boa dica,
    Concordo quando dizes que as Expressões Regulares são muito pouco conhecidas e ainda menos usadas..
    Como exemplo, tento usar ER sempre que possível e evito tê-las hardcoded, costumo ter tabelas de parametrização onde se colocam as filtros/expressões, um dos exemplos pode ser para filtrar documentos financeiros pelo texto do item, em que queremos filtro com o formato “PAGAMENTOxxx” (sendo o “xxx” 3 dígitos), a expressão seria “PAGAMENTO\d{3}”.

    PS: fora ABAP, podem usar ER também na pesquisa do notepad++, as possibilidades são imensas :)

  2. Nuno Godinho Diz:

    Boa! É bom saber que alguém dá uso digno às Expressões Regulares!

  3. RSousa Diz:

    Ehehe,

    Acredita que sempre que posso ensino isto a todos, essencialmente através do notepad++.

  4. Adriano Diz:

    show de bola Nuno! Periodicamente entro no seu blog pra ver as novidades e quase sempre aprendo alguma coisa nova contigo.
    Parabéns e obrigado!

  5. Nuno Godinho Diz:

    Obrigado Adriano. É bom saber isso :)

Deixe um comentário


About Abapinho
Abapinho runs on WordPress
Articles (RSS) e Comments (RSS).