Search code examples
pythonparsingsyslog-ng

How to remove a string, between two delimiters, at the end of a line?


So I want to remove the last statement in my rule, the rule structure goes like this:

<pattern> @rule statements go here@ @multiple rule statements@ @remain all on the same line@</pattern>

The parsing statements will always be between the @ characters, and I would like to remove the last statement in the line.

I can use regex to remove everything in-between the @ characters:

re.sub(r'@.+?@', '', s)

How can I make that happen for only the last statement in the line, when each line will be different from one another?


Solution

  • (\@[^\@]+)\@?$
    

    The above regex will search for the last occurrence of @ then work backwards to achieve a full match of the last @string@ occurrenc, in the example string below "@remain all on the same line@" will be matched

    @rule statements go here@ @multiple rule statements@ @remain all on the same line@