Home > Software engineering >  RegEx to match text outside of square and curved brackets
RegEx to match text outside of square and curved brackets

Time:05-10

I have a log file that I need to search for. The text outside of the brackets is always the same, so to be as precise as I can, I would like to ignore the text inside both square and curved brackets.

So the log is;

[09/May/2022:11:04:05  0000] NSMMReplicationPlugin - process_postop: Failed to apply update (6278e86f000627440000) error (-1).  Aborting replication session(conn=5361 op=6)

Where I do not need the date at the start inside the [ ] or the numbers & text inside both sets of ( ) either side of the (-1) as the (-1) part is key to the search.

I just need to search on;

NSMMReplicationPlugin - process_postop: Failed to apply update error (-1).  Aborting replication session

I cannot figure this out! I tried a lazy qualifier but as there's ( ) that I need it didn't work;

(?<=NSMMReplicationPlugin - process_postop: Failed to apply update)(.*)(?=error (-1). Aborting replication session)

I somehow need to escape the ( ) to get the -1 part?

CodePudding user response:

We can use the following pattern which divides the text into 4 groups and only uses group 2 and 4.

(\[.*\])(.*)(\(\w*\))(.*)

to get group 2 and 4 we use \2\4 or $2$4 depending on the flavour of regex.
See https://regex101.com/r/v6izps/1 for an example.
Depending on the language and the tools there may not be any need to put brackets around the 1st and 3rd groups.

  • Related