VBA Regular Expressions - Extract substring

Asked

Viewed 571 times

6

In the text below, I would like a regular Pattern Expression, compatible with VBA, to extract the text located between the equal sign "=" and the first line break (highlighted in bold):
Client = CON CLIENTE ABCD

Rule = Allocation in committed Limit - Min. R$ 0 - ( 0,00% ) - Max. R$ 0 - ( 0,00% ) Base Value - R$ 0,00 Financial Calculated Value - R$ 0,00 - ( 0,00% )

  • 2

    and customers repeat themselves or not? I can’t understand the question like this, it’s too wide.

  • 1

    Client = s+?(.*?) s+? n

  • Which Office program is being used in VBA?

  • With a test in Excel this was the expression used in which it was possible to extract the data in bold: (?:Cliente =\s+?)(.*?)\s+?(?:Regra.+$) and the regex test. With Cliente =\s+?(.*?)\s+?\n It seems that Excel does not recognize the \n new line.

  • @Guilhermenascimento, customers do not repeat themselves.

  • @dosSantos I refer to "key", I mean is a customer only or multiple customers?

Show 1 more comment

1 answer

1

[...]would like a Pattern of regular Expression, compatible with VBA, to extract localized text between the equals sign "=" and the first line break [...]

There are 2 ways to capture this:

  • You can use this regex:

    ^.*?= *(.*)

  • Or use this regex no global flag /g (check if this activated, usually comes activated by default).

    = *(.*)

The two will return everything after the = ignoring the spaces between it and the first character.
You can see how the first example works here
You can see how the 2nd example works here


Why not simply use the "Client =" sequence before?

Using this sequence would definitely give the same result, however only in this example.
As the purpose of the user who asked is "capture between equals signal "=" and the first line break [...]" cannot be done based on the string in the first line of the example
I believe it’s right to use tokens which represent these conditions as delimiters for catching, so regex will present the desired results independent of changes in the sequence before the first =.

Browser other questions tagged

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