简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM