P
P
Pavel Karinin2020-08-11 20:38:37
Parsing
Pavel Karinin, 2020-08-11 20:38:37

How to extract all values ​​before, between and after curly braces, excluding the braces themselves?

Greetings. Not strong in regular expressions, due to the rare need to use them, but here I had to ... In fact, you need to extract all the values ​​\u200b\u200bbefore, between and after curly brackets, excluding the curly brackets themselves. There is a string whose format is quite strict and is represented as follows:

разделитель{заполнитель}разделитель{заполнитель}разделитель

Conditions:
  • a string can only start with a delimiter ;
  • the string can end with either a delimiter or a placeholder ;
  • the chain of alternating filler and separator can be arbitrarily long;
  • the placeholder is always in paired curly braces;

Additional terms:
  • both placeholder and separator can contain only: latin letters, numbers, dot, dash, slash and underscore;
  • both placeholder and delimiter cannot begin or end with a non-alphabetic character, and cannot contain two or more consecutive non-alphabetic characters;

Perhaps it makes sense to check the placeholder and separator for compliance with additional conditions after they have been extracted from the string. In any case, the string will be declared invalid if at least one condition is violated. In this case, it is necessary to compose a regular expression (if possible), with named groups (such as filler and separator), for more convenient extraction of substrings.

By the way, performance does not matter very much, because. the operation will be performed quite rarely and with a very small number of lines (no more than 50-100), while the length of the lines will not exceed 200-300 characters. Or maybe it's easier and faster to write a parser?

I want to make a reservation right away, knowing the favorite saying of local authorities " Google to the rescue”, that I came across different solutions, but they are partial, I can’t put the whole thing together, i.e. into an entire regular expression. Thanks in advance for your replies and assistance.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question