Regular expression HELP get multiply string

I am having difficult parse the specific string
[20221018.165317.401606][info]:[DL- UE[ 0]: Value= 0.000091 Mbps, Mcs= 4.0(Sigma= 0.0), Num= 1.4]

How do I get the string 20221018.165317.401606 Value= 0.000091 Mbps Mcs= 4.0 Num= 1.4

I only able to parse the first string the timedate

[2]\d{7}.\d{6}.\d{6}
or
[([0-9.]+)]

string="[20221018.165317.401606][info]:[DL- UE[ 0]: Value=    0.000091 Mbps, Mcs=  4.0(Sigma= 0.0), Num=   1.4]"
regex = re.compile(r'[2]\d{7}\.\d{6}\.\d{6}') #'[2] start with 2
#regex=re.search(r"\[([0-9.]+)\]", string)
match = regex.search(string)
print(match.group(0)) #20221018.165317.401606

I try with this \[([0-9.]+)\]([A-Z][a-z]{4}) not working, any idea to parse it
I try with this work ([0|1]\.\d[0-9]+) ==>0.000091 Mbps

I’d probably go with \[(\d+\.\d+\.\d+)\].*?(Value=[^]]+) and then use the contents of groups 1 and 2.

like this: m3 = re.search(r'\[(\d+\.\d+\.\d+)\].*?(Value=[^]]+)', st2)

I try with this

m3 = re.search(r'\[(\d+\.\d+\.\d+)\].*?(Value=[^]]+)', string)
print(m3.group(2)) 
#output: Value=    0.000091 Mbps, Mcs=  4.0(Sigma= 0.0), Num=   1.4

How to get the value, using split

If you mean the value after "Value=", then:

>>> m3.group(2).split()
['Value=', '0.000091', 'Mbps,', 'Mcs=', '4.0(Sigma=', '0.0),', 'Num=', '1.4']
1 Like

HI do you know how to split like this:
string: [20221110.112127.316856][info]:[PDCP DL- ingress traffic: 0.000000(Mbps), egress traffic: 0.000000(Mbps)]

How can I split like [ ‘ingress traffic:’ , ‘0.000000(Mbps)’, ‘egress traffic:’, ‘0.000000(Mbps)’]

i try like this

str = "[20221110.112127.316856][info]:[PDCP DL- ingress traffic: 0.000000(Mbps), egress traffic: 0.000000(Mbps)]"
search3 = re.search(r'ingress [^]]+', str) 
nn= search3.group()
nn

output:‘ingress traffic: 0.000000(Mbps), egress traffic: 0.000000(Mbps)’

How can i achieve like below result:

[ 'ingress traffic:' , '0.000000(Mbps)', 'egress traffic:', '0.000000(Mbps)']

Because there are multiple space so when using split with space will spit all the space, but i actually only want the second space

is there any solution to it?

i solve it

string2="[20221110.112127.316856][info]:[PDCP DL- ingress traffic: 0.000000(Mbps), egress traffic: 0.000000(Mbps)]"
searchtest=re.search(r'(ingress [^(]+).+(egress [^(]+)',string2)
test= searchtest.group(1)+", "+ searchtest.group(2)
#ingress traffic: 0.000000, egress traffic: 0.000000
test1=test.replace(", ", ":").strip().split(':')
['ingress traffic', ' 0.000000', 'egress traffic', ' 0.000000']

Some remarks:

If I were doing this, I’d be inclined to match more rigidly. For
example, instead of just grabbing everything which is not an opening
bracket, grab the labels (eg “ingress traffic”), the delimiter (": ")
and the numeric parts (“0.000000”) specificly. Maybe even the units
inside the brackets. This avoids mismatching incorrectly/inappropriate
lines, and also makes it easier to deal with the matched parts.

Example (untested):

 (ingress traffic):\S+(\d+\.\d+)

This (a) matches your label specificly and (b) matches a number. It also
puts groups (the brackets) around the label and number; those groups are
then available in the match object directly (in your code, the
searchtest variable - the result of the re.search call).

Then you can for example use the label as a key in a dict and convert
the numeric string to an actual number, eg:

 label = searchtest.group(0)
 numeric = searchtest.group(1)
 number = float(numeric)

Cheers,
Cameron Simpson cs@cskk.id.au

i used your method, is it like this:

re.search(r’(ingress traffic):\S+(\d+.\d+)', str)

but nothing is found, did I miss it?

Ahh… I’m an idiot. I mean \s (whitespace), not \S (nonwhitespace).
Try a lowercase s in the above regexp.

When you have that working, you can do things like a loop for the
various labels (to use the same code), or re.findall() with a pattern to
match any label, then look up the labels it finds (or just build a
dict with keys from all the labels).

Sorry,
Cameron Simpson cs@cskk.id.au

thanks
but I wish to do like this
[ ‘ingress traffic:’ , ‘0.000000(Mbps)’, ‘egress traffic:’, ‘0.000000(Mbps)’]

Well nothing says you need to put things into a dict, you can just append the label and the numeric part onto your list of results. And you can make the pattern for the numeric part include the bracketed unit (Mbps) as desired.

Cheers,
Cameron

ok thanks for the help