仅匹配匹配的块(如果起始单词出现在匹配块之前,则忽略)

时间:2014-07-07 09:22:27

标签: ruby regex rubular

我的输入如下,

输入

StatusMsg:
seqId: 14043
timestamp: 140707
dId: "Sa01"
msgType: SEQUENCE
eventType: UPDATE_CMD
devContext {
   context: IDLE
   operationalMode: 1
   logHistory {
      start: 1404387563607
      end: 1404387563616
   }
}
manifest {
   timestamp: 1404387733059
   dev {
      dId: "Sa01"
      mainComponent {
         serialNum: "10001"
         deviceClass: "Sap"
         componentType: "Sa01"
         subcomponentNum: 1
      }
   }
   info {
      name: "Sa01"
      channels: 1
      lib {
         name: "l.bin"
         timestamp: 1404387733059
         version: "6870711ee"
         validStartDate: 1404387733059
      }
      activeSW {
         name: "Sa01"
         timestamp: 1404387733059
         version: "12.001"
         validStartDate: 1404387733059
      }
      activeCSW {
         name: "Sa01"
         timestamp: 1404387733059
         version: "03.001"
         validStartDate: 1404387733059
      }
   }
}
powerStatus {
   powerEvent: ON
   powerSource: AC
   chargerStatus: CHARGING
   batteryStatus: GOOD
}
status {
   msgHeader {
      messageId: 13
      timestamp: 1404387733059
      seqNum: 13
      sourceId: 13
      numOfParams: 0
   }
   infuserState: READY
   keypadLockout: UNLOCKED
}
cmdResponse {
   cmdType: A_PGM
   aPgm {
      dId: "Sa01"
      refId: 79
      request {
         refId: 79
         libId: "6870711eedb"
         channelId: 1
         lId: 1
         pgmType: FULL
         aPgmType: NEW
         externalId: "EXT_ID_9"
         stats {
            WG: 76230
            HG: 150500
            BS: 1790
         }
         multi: false
      }
      response {
         dId: "Sa01"
         files: 1.bin
         major: 338013710701
         status: Received
      }
   }
}
[2014-07-07 14:10:41.034] I/O Received
StatusMsg:
seqId: 14043
timestamp: 140707
dId: "Sa01"
msgType: SEQUENCE
eventType: UPDATE_CMD
devContext {
   context: IDLE
   operationalMode: 1
   logHistory {
      start: 1404387563607
      end: 1404387563616
   }
}
manifest {
   timestamp: 1404387733059
   dev {
      dId: "Sa01"
      mainComponent {
         serialNum: "10001"
         deviceClass: "Sap"
         componentType: "Sa01"
         subcomponentNum: 1
      }
   }
   info {
      name: "Sa01"
      channels: 1
      lib {
         name: "l.bin"
         timestamp: 1404387733059
         version: "6870711ee"
         validStartDate: 1404387733059
      }
      activeSW {
         name: "Sa01"
         timestamp: 1404387733059
         version: "12.001"
         validStartDate: 1404387733059
      }
      activeCSW {
         name: "Sa01"
         timestamp: 1404387733059
         version: "03.001"
         validStartDate: 1404387733059
      }
   }
}
powerStatus {
   powerEvent: ON
   powerSource: AC
   chargerStatus: CHARGING
   batteryStatus: GOOD
}
status {
   msgHeader {
      messageId: 13
      timestamp: 1404387733059
      seqNum: 13
      sourceId: 13
      numOfParams: 0
   }
   infuserState: READY
   keypadLockout: UNLOCKED
}
cmdResponse {
   cmdType: A_PGM
   aPgm {
      dId: "Sa01"
      refId: 79
      request {
         refId: 79
         libId: "6870711eedb"
         channelId: 1
         lId: 1
         pgmType: FULL
         aPgmType: NEW
         externalId: "EXT_ID_9"
         stats {
            WG: 76230
            HG: 150500
            BS: 1790
         }
         multi: false
      }
      response {
         dId: "Sa01"
         files: 1.bin
         major: 35723057325
         status: Valid
      }
   }
}
[2014-07-07 14:15:71.028] I/O Received
StatusMsg:
seqId: 14043
timestamp: 140707
dId: "Sa01"
msgType: SEQUENCE
eventType: UPDATE_CMD
devContext {
   context: IDLE
   operationalMode: 1
   logHistory {
      start: 1404387563607
      end: 1404387563616
   }
}
manifest {
   timestamp: 1404387733059
   dev {
      dId: "Sa01"
      mainComponent {
         serialNum: "10001"
         deviceClass: "Sap"
         componentType: "Sa01"
         subcomponentNum: 1
      }
   }
   info {
      name: "Sa01"
      channels: 1
      lib {
         name: "l.bin"
         timestamp: 1404387733059
         version: "6870711ee"
         validStartDate: 1404387733059
      }
      activeSW {
         name: "Sa01"
         timestamp: 1404387733059
         version: "12.001"
         validStartDate: 1404387733059
      }
      activeCSW {
         name: "Sa01"
         timestamp: 1404387733059
         version: "03.001"
         validStartDate: 1404387733059
      }
   }
}
powerStatus {
   powerEvent: ON
   powerSource: AC
   chargerStatus: CHARGING
   batteryStatus: GOOD
}
status {
   msgHeader {
      messageId: 13
      timestamp: 1404387733059
      seqNum: 13
      sourceId: 13
      numOfParams: 0
   }
   infuserState: READY
   keypadLockout: UNLOCKED
}
cmdResponse {
   cmdType: A_PGM
   aPgm {
      dId: "Sa01"
      refId: 79
      request {
         refId: 79
         libId: "6870711eedb"
         channelId: 1
         lId: 1
         pgmType: FULL
         aPgmType: NEW
         externalId: "EXT_ID_9"
         stats {
            WG: 76230
            HG: 150500
            BS: 1790
         }
         multi: false
      }
      response {
         dId: "Sa01"
         files: 1.bin
         major: 27151510570
         status: Accepted
      }
   }
}
[2014-07-07 14:15:51.034] I/O Received
StatusMsg:
seqId: 14043
timestamp: 140707
dId: "Sa01"
msgType: SEQUENCE
eventType: UPDATE_CMD
devContext {
   context: IDLE
   operationalMode: 1
   logHistory {
      start: 1404387563607
      end: 1404387563616
   }
}
manifest {
   timestamp: 1404387733059
   dev {
      dId: "Sa01"
      mainComponent {
         serialNum: "10001"
         deviceClass: "Sap"
         componentType: "Sa01"
         subcomponentNum: 1
      }
   }
   info {
      name: "Sa01"
      channels: 1
      lib {
         name: "l.bin"
         timestamp: 1404387733059
         version: "6870711ee"
         validStartDate: 1404387733059
      }
      activeSW {
         name: "Sa01"
         timestamp: 1404387733059
         version: "12.001"
         validStartDate: 1404387733059
      }
      activeCSW {
         name: "Sa01"
         timestamp: 1404387733059
         version: "03.001"
         validStartDate: 1404387733059
      }
   }
}
powerStatus {
   powerEvent: ON
   powerSource: AC
   chargerStatus: CHARGING
   batteryStatus: GOOD
}
status {
   msgHeader {
      messageId: 13
      timestamp: 1404387733059
      seqNum: 13
      sourceId: 13
      numOfParams: 0
   }
   infuserState: READY
   keypadLockout: UNLOCKED
}
cmdResponse {
   cmdType: A_PGM
   aPgm {
      dId: "Sa01"
      refId: 79
      request {
         refId: 79
         libId: "6870711eedb"
         channelId: 1
         lId: 1
         pgmType: FULL
         aPgmType: NEW
         externalId: "EXT_ID_9"
         stats {
            WG: 76230
            HG: 150500
            BS: 1790
         }
         multi: false
      }
      response {
         dId: "Sa01"
         files: 1.bin
         major: 35723057325
         status: Valid
      }
   }
}

我想确保以相同的顺序

在输入中必须存在以下内容
StatusMsg:
dId: "Sa01"
eventType: UPDATE_CMD
cmdResponse
cmdType: A_PGM
response
dId: "Sa01"
status: Accepted

预期输出

StatusMsg:
seqId: 14043
timestamp: 140707
dId: "Sa01"
msgType: SEQUENCE
eventType: UPDATE_CMD
devContext {
   context: IDLE
   operationalMode: 1
   logHistory {
      start: 1404387563607
      end: 1404387563616
   }
}
manifest {
   timestamp: 1404387733059
   dev {
      dId: "Sa01"
      mainComponent {
         serialNum: "10001"
         deviceClass: "Sap"
         componentType: "Sa01"
         subcomponentNum: 1
      }
   }
   info {
      name: "Sa01"
      channels: 1
      lib {
         name: "l.bin"
         timestamp: 1404387733059
         version: "6870711ee"
         validStartDate: 1404387733059
      }
      activeSW {
         name: "Sa01"
         timestamp: 1404387733059
         version: "12.001"
         validStartDate: 1404387733059
      }
      activeCSW {
         name: "Sa01"
         timestamp: 1404387733059
         version: "03.001"
         validStartDate: 1404387733059
      }
   }
}
powerStatus {
   powerEvent: ON
   powerSource: AC
   chargerStatus: CHARGING
   batteryStatus: GOOD
}
status {
   msgHeader {
      messageId: 13
      timestamp: 1404387733059
      seqNum: 13
      sourceId: 13
      numOfParams: 0
   }
   infuserState: READY
   keypadLockout: UNLOCKED
}
cmdResponse {
   cmdType: A_PGM
   aPgm {
      dId: "Sa01"
      refId: 79
      request {
         refId: 79
         libId: "6870711eedb"
         channelId: 1
         lId: 1
         pgmType: FULL
         aPgmType: NEW
         externalId: "EXT_ID_9"
         stats {
            WG: 76230
            HG: 150500
            BS: 1790
         }
         multi: false
      }
      response {
         dId: "Sa01"
         files: 1.bin
         major: 27151510570
         status: Accepted
      }
   }
}

我尝试使用正则表达式

(StatusMsg:.*?dId:\s*"Sa01".*?eventType: UPDATE_CMD.*?response\s*{[\s\n\w\.:]*dId:\s*"Sa01"[\s\n\w\.:]*status:\s*Accepted)

上述表达式从第一个 StatusMsg 中选择,直到已接受。任何人都可以帮助构建正确的正则表达式吗?

1 个答案:

答案 0 :(得分:1)

您只需要对正则表达式进行一些小改动即可。请注意,使用正则表达式时,最后还需要m标记。

更新的正则表达式:

/(StatusMsg:(?!.*StatusMsg:).*?dId:\s*"Sa01".*?eventType: UPDATE_CMD.*?response\s*{[\s\n\w\.:]*dId:\s*"Sa01"[\s\n\w\.:]*status:\s*Accepted)/m

注意:要使用rubular进行测试,请在开头删除/,在结尾删除/m,然后将m放在文本框右侧的小文本框中你正在进入你的正则表达式。这会将m标记设置为适用,这会使正则表达式中的.与新行匹配(而不是默认行为)。

唯一改变的是

StatusMsg:.*?

已成为

StatusMsg:(?!.*StatusMsg:).*?

这适用于负向前瞻。这实际上是告诉你的正则表达式只匹配正则表达式匹配的位置,但仅当匹配不包含两个字符串StatusMsg时才会匹配。

如果StatusMsg始终位于行的开头而没有前面的空格,则可以通过执行以下操作来使其更具体:

^StatusMsg:(?!.*^StatusMsg:).*?