Home > Software engineering >  regex on couple of lines
regex on couple of lines

Time:04-01

With this text string:

leonardo.vid.0=0x2341
leonardo.pid.0=0x0036
leonardo.vid.1=0x2341
leonardo.pid.1=0x8036
leonardo.vid.2=0x2A03
leonardo.pid.2=0x0036
leonardo.vid.3=0x2A03
leonardo.pid.3=0x8036

I want to search for a match with the following rules:

  • given the first label (say leonardo)
  • given vid and a pid (say 0x2341 and 0x8036)
  • they must be on the same couple of lines (i.e. vid.1 and pid.1)

I'm interested to understand if a match exists.

I'm able to manually parse the string and use some logic to achieve the goal. But I wonder if there is a regexp that can do this for me.

CodePudding user response:

You may use the following regex pattern:

leonardo\.vid\.(\d )=0x2341\nleonardo\.pid\.\1=0x8036

This pattern matches a first vid line with a value of 0x2341, followed by a pid line with the same ID number, and the value 0x8036.

Demo

  • Related