Home > Software engineering >  Regex : Get pattern which is between multiple patterns to be matched
Regex : Get pattern which is between multiple patterns to be matched

Time:10-03

Say I have a string

versioned object base "/cm/common" (locked)
  created 2002-08-22T11:43:31 02:00 by Super-User (root.other@bb-soft-ccs)
  VOB family feature level: 4
  VOB storage host:pathname "atpccupd.cvc-global.net:/export/vobstore/obce/cm_common.vbs"
  VOB storage global pathname "/net/atpccupd.cvc-global.net/export/vobstore/obce/cm_common.vbs"
  database schema version: 80
  modification by remote privileged user: allowed
  atomic checkin: disabled
  VOB ownership:
    owner tmn/ccadm
    group tmn/HBGHS_CC_G
  Additional groups:
    group tmn/smc_cc
  ACLs enabled: No
  Attributes:
    FeatureLevel = 4

In the second line you see we have created 2002-08-22T11:43:31 02:00 by Super-User (root.other@bb-soft-ccs). I need to extract Super-User from here.

I need to write a regex such that first it matches created then the first date time like pattern then by and then get the string after that. The last string in the same line could either be a end or an email which needs to be pattern matched two.

So what I need basically is

r'created (matchdateTtime)by(matchtheuser)($orEmailMatchWithinBrackets)'

This is what I tried

import re
re.search(r'created (\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2} \d{2}:\d{2})by(.*?)(emailmatchpattern|$)', text)

But this does not seem to be working.

Here is my email match pattern : ^[\w-\.] @([\w-] \.) [\w-]{2,4}$

CodePudding user response:

try:

import re
re.search("(?<=created\s\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d\ \d\d:\d\d\sby\s).*(?=\s\()", text)
  • Related