There is a text: h1;some text in h1;h1 . How to re using h1; before and after the text?

From comments:
We need to get the values ​​of h1. But instead of h1 there may be other characters.
That is, you just need to "throw away" the text between these characters.

  • What does "pull out" mean? Remove? Replace? Get the values? - Sergey Gornostaev
  • need to paint in more detail. Pull out what is already known there is no point. - KoVadim
  • @SergeyGornostaev Yes, get the values ​​of h1 . But instead of h1 there may be other characters. - CockLobster
  • @SergeyGornostaev That is, you just need to "throwing" the text between these characters. - CockLobster
  • whether some text in h1 contain ; inside? - jfs

3 answers 3

If the text between the characters ';' you just need to throw it away, then the solution is elementary

 import re re.sub(r';.*;', ';;', 'h1;some text in h1;h1') 
  • In the condition it was written that instead of h1 there may be other characters (for example: p1, d1, z1). - CockLobster
  • one
    And the example given by me will process them, you can check. The re.sub function takes the regular expression as the first parameter, substitutes the second, and the third that needs to be processed. - Sergey Gornostaev

https://regex101.com/r/nG8wH9/2

 import re re.sub(r'([\w\d]+;)(.*?)\1', r'\1\1', 'h1;some text in h1;h1') 

    The simplest solution would be this:

     start_h, _, end_h = line_for_parse.split(';') assert start_h == end_h