SRS
SRS

Reputation: 449

Using Regex to match a list of alphanumeric characters in python

I am trying to parse through a file line by line and look out for packet data being sent out by the processor which are essentially represented in alphanumeric characters. I wrote a regex in Python to read the pattern and store the packet data in a list.

A sample line:

Date Time ProcessName ActivityName : 55 34 00 aa c9 00 11 45 55

My regex:

r'([^\s]*?)\s([^\s]*?)\s([^\s]*?)\s([^\s]*?)\s(R.*?:)\s(\d|\D|\s)+$'

I have to add the packet data [the numbers shown after :] into a list and do some pattern process activity. When I run my script and print match.group(6) it is just printing me a bunch of '\n' s inside a list.

A snippet of my script:

regex = r'([^\s]*?)\s([^\s]*?)\s([^\s]*?)\s([^\s]*?)\s(R.*?:)\s(\d|\D|\s)+$'
pattern = re.compile(regex) 

for line in content:
    match = pattern.search(line)
    if match:
       print match.group(6)

How should i read a set of alphanumeric characters using regex?

Upvotes: 1

Views: 855

Answers (1)

vks
vks

Reputation: 67968

You can directly take that out using re.findall.

 (?<=:)\s*([\da-zA-Z]{2}(?:\s[\da-zA-Z]{2})*)

See demo.

https://regex101.com/r/eZ0yP4/13

Upvotes: 3

Related Questions