Kirk ERW
Kirk ERW

Reputation: 167

regex find what's after slash without showing the slash

I have a script that split the informations in a line, i've succeeded to split it and extract the informations i need but i have the slash that shows up :/ i'de like to get only what's after it here's an example:

import re 

data = "12:05:12.121    O:CLASS (SMS:/[email protected]) R:VOICE/1654354 mid:4312" 
ms = re.match(r'(\S+).*mid:(\d+)', data) # extract time and mid 
k = re.findall(r"/\S+", data ) # extract source and destination 
result = { 'time':ms.group(1), 'mid':ms.group(2), "source":k[0],"Destination":k[1]}

print result 

and here's the result {'source': '/[email protected])', 'Destination':'/1654354', 'mid':'4312','time':'12.05.12.121'}

and the result i want is without slash like here:

{'source': '[email protected])', 'Destination':'1654354', 'mid':'4312','time':'12.05.12.121'} 

Upvotes: 0

Views: 96

Answers (1)

Blender
Blender

Reputation: 298106

Wrap the \S+ in a capturing group:

k = re.findall(r"/(\S+)", data)

And here's another way of getting that info all with one regex:

import re 

data = "12:05:12.121      O:CLASS (SMS:/[email protected]) R:VOICE/1654354 mid:4312" 
result = re.search(r'''
      (?P<time>.*?)
      \s+
      .*?
      \s+
      \(
          (?P<type>.*?):/(?P<source>.*?)
      \)
      \s+
      .*/(?P<destination>\d+)
      \s+
      mid:(?P<mid>\d+)
''', data, re.VERBOSE)

print result.groupdict()

Upvotes: 3

Related Questions