Continuing the theme:

Regular expression, text replacement

Help modify the regular expression: (?<=1)\d(\d)\d(?=5) so that you can get multiple internal expressions.

For example, the source text: 12345 is converted to 135 (as in the previous example). However, if it is necessary to perform a replacement for several internal expressions: 12342642845, then a match does not occur (the result should be 13685). Figures 2 and 4 can also be considered as reference points.

Is it possible to somehow achieve the universality of the expression to work on any number? You can of course create different expressions for different amounts of nested elements, but it seems to me that this is some kind of crutch.

  • one
    You would formulate in words the exact rules of how cutting should take place. as it now turns out that it is enough just to throw out all 2 and 4. i.e. literally a global replacement [24] for nothing - Mike
  • Almost correctly caught the point. Only not globally throw out 2 and 4, and within the limits of 1 and 5. - Pincher1519
  • Do your constructions \G and \K work? If so, then in global mode we change (1|(?!^)\G).*?\K[24](?=.*?5) to nothing. - Mike
  • 1) cut the part of the line between 1 and 5 ( ^[^1]*(1\d*5)[^5]*$ ). 2) From the result, delete all 2 and 4 ( /[24]// ). In general, in order to understand how a regular example of one example should work is usually not enough. - andy.37
  • one
    @ andy.37 I understand the question that you don’t need to touch anything outside of 1 and 5, only between them - Mike

2 answers 2

Went the other way.

In the end, he made two calls to different regulars. First I removed 42 matches inside 15, and then removed 24 inside 15. It’s a pity that we didn’t manage to do it in one regular expression.

    Everything from one to five, not including them, throwing a digit at each end?

     (?<=1)\d(\d*)\d(?=5) 

    And you can also replace

     (1)\d(\d*)\d(5) 

    on

     \1\2\3 

    or (in other languages)

     $1$2$3 
    • No, the task is somewhat more complicated. I used numbers to make it easier to describe the regular schedule. You can try with the brackets: [A] ([B] [C] [D]) [E] The result should be: [A] (BCD) [E] - Pincher1519
    • @ Pincher1519, incomprehensible. - Qwertiy
    • In the round brackets we kill square ones; outside the round ones we don’t kill. - Pincher1519
    • 2
      It seems that balancing group definition goo.gl/PUiBqi will do for this . There, in the examples, brackets catch (and nested). I still did not figure it out with him, it seemed difficult. - tsul