Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match and replace string with multiple lines Python

Tags:

python

regex

I need help to match 2 strings and replace with empty string ' '. Appreciate your help as i am still new in Python and coding:

crypto pki certificate chain TP-self-signed-1357590403
  +30820330 30820218 A0030201 02020101 300D0609 2A864886 F70D0101 05050030
  +31312F30 2D060355 04031326 494F532D 53656C66 2D536967 6E65642D 43657274
  +69666963 6174652D 31333537 35393034 3033301E 170D3139 30313234 31353436
  +34345A17 0D323030 31303130 30303030 305A3031 312F302D 06035504 03132649
  +4F532D53 656C662D 5369676E 65642D43 65727469 66696361 74652D31 33353735
  +39303430 33308201 22300D06 092A8648 86F70D01 01010500 0382010F 00308201
  +0A028201 0100E69D C133454E 401E763A 7686E453 5D58020D 0E6E122F A0F19E15
  +E0975148 666110BD C1F09B86 CB701C20 EF85E024 F759A921 D11DA10C A13BA3BD
  +20006387 917287CE EA0CFDDC 2FA5DD07 E5B200F4 108CACA1 DCEF0E4E EEE908ED
  +2ACD693B FC90A24F 9F865CB9 859FEFB0 EB8904D4 8FA83D29 E93B892F 32F3EC7D
  +EAA2850E 1793BBCE 86EA47B2 15645634 D81EA89C 1C2BC092 766DF58F 0B289A82
  +0C92E551 7AA9588E F5B41A41 6DB4C785 101E674D BBBCFB42 9F4F9A25 70389515
  +D1C07E2F 18C0557D 95283E90 3CCD2966 5EBF5668 A6B0B847 0B278906 E5BFA668
  +EFBE938A BE70C4C0 1A8D7218 71463EA5 49540A45 DF307B4C 459E657D C039BB68
  +F047B0B2 2F250203 010001A3 53305130 0F060355 1D130101 FF040530 030101FF
  +301F0603 551D2304 18301680 141FADF3 CC2C2293 810EDAA8 9E55327C D2B7D88A
  +88301D06 03551D0E 04160414 1FADF3CC 2C229381 0EDAA89E 55327CD2 B7D88A88
  +300D0609 2A864886 F70D0101 05050003 82010100 91E63F44 376F91C1 C50C08E4
  +B29B902B B1BC7831 C5607897 030835A6 108FC1F2 6F3DEE23 EF3E8FFF 81A121B5
  +26596004 F8F61DFD 1B603C5D 42D850E6 439C7CAE BFC285AE 3FD83870 125594C0
  +51EAAC09 BF42446F C6399B90 D0E10ACA B208819B 645BECE5 DBDDA9AD EBA1FCD9
  +2B14D0DE AB2AC1BF FF064076 ADBB4540 17AB77A4 C6B0DA3B 1BC0F5B8 44030E7B
  +27318CEE 14C90739 DD8684A8 9346EEC1 3F4958EF 835BA822 F58523C9 E9F83105
  +D3E68700 20DAFC5E B1B8CF5B BAC5CEB3 00321088 43125173 51FC8006 270731E6
  +0E0C6183 68BABA99 BD9F4F28 1EDA82D4 F00F1359 F30B6501 BC468C89 49111AB2
  +CBDE5A9D DB8DB33A 45FE6C96 7D49A70F 4C299618

Will always have 27 lines starting with first line

Second is:

crypto pki certificate chain TP-self-signed-1357590403
 -certificate self-signed 01 nvram:IOS-Self-Sig#1.cer
like image 585
Ivan Madolev Avatar asked Apr 02 '19 07:04

Ivan Madolev


People also ask

Which flag will search over multiple lines in Python?

The re. MULTILINE flag tells python to make the '^' and '$' special characters match the start or end of any line within a string. Using this flag: >>> match = re.search(r'^It has.

What is the meaning of \1 in regex in Python?

The first \1 means the first group - i.e. the first bracketed expression (\b[a-z]+) From the docs \number. "Matches the contents of the group of the same number. Groups are numbered starting from 1. For example, (.+) \1 matches 'the the' or '55 55', but not 'thethe' (note the space after the group)"

How to match a regex pattern inside a string in Python?

Python re.match () method looks for the regex pattern only at the beginning of the target string and returns match object if match found; otherwise, it will return None. In this article, You will learn how to match a regex pattern inside the target string using the match (), search (), and findall () method of a re module.

How to replace a string in Python with regular expression?

If you want to replace the string that matches the regular expression instead of a perfect match, use the sub () method of the re module. To replace a string in Python using regex (regular expression), use the regex sub () method. The re.sub () is a built-in Python method that accepts five arguments maximum and returns replaced string.

How to replace a pattern in Python with a substitute string?

Python regex offers sub () the subn () methods to search and replace patterns in a string. Using these methods we can replace one or more occurrences of a regex pattern in the target string with a substitute string.

How to replace one or more occurrences of a regex pattern?

Using these methods we can replace one or more occurrences of a regex pattern in the target string with a substitute string. After reading this article you will able to perform the following regex replacement operations in Python. Before moving further, let’s see the syntax of the sub () method.


Video Answer


1 Answers

If you want to match the line including the next line, you could match all the lines and use a negative lookahead to assert that the next line does not start with crypto.

Then match a newline and crypto until the end of the line:

^crypto pki certificate chain TP-self-signed-.*(?:\n(?!crypto).*)*\ncrypto.*

Regex demo

If the starting line should be the same as the line at the end you could use a capturing group for the first line with a backreference:

^(crypto pki certificate chain TP-self-signed-.*)(?:\n(?!\1).*)*\n\1

Regex demo

Your code could look like

pattern = r'^(crypto pki certificate chain TP-self-signed-.*)(?:\n(?!\1).*)*\n\1'
df=re.sub(pattern, '' , file, 0, re.MULTILINE)
like image 104
The fourth bird Avatar answered Nov 03 '22 18:11

The fourth bird