Home > Back-end >  Split Data by Ignoring Semicolon Inside Square Bracket in Python
Split Data by Ignoring Semicolon Inside Square Bracket in Python

Time:04-01

I want to split the data by Semi Colon, But i need to ignore the semi colon inside the Square Bracket.


val = "L=1nH;Tol=0.3nH;Idc=1000mA;Isat=NA;Iacpp=NA;Fr=10GHz;ESR=0.10ohm;Q=[100MegHz 7; 500MegHz 23;1.8GHz 47;2.4GHz 57];dL@Isat=NA;dT@Idc=NA"
val.split(";")

CodePudding user response:

Using re.findall we can try:

val = "L=1nH;Tol=0.3nH;Idc=1000mA;Isat=NA;Iacpp=NA;Fr=10GHz;ESR=0.10ohm;Q=[100MegHz 7; 500MegHz 23;1.8GHz 47;2.4GHz 57];dL@Isat=NA;dT@Idc=NA"
parts = re.findall(r'\w =\[.*?\]|\w =[^;] ', val)
print(parts)

The regex pattern used here says to match:

\w =\[.*?\]  a key = a term in [...]
|            OR
\w =[^;]     a key = some other term

The regex alternation places the key=[...] variant first, so that we avoid trying to match to semicolons internal to the bracketed value.

CodePudding user response:

You could split on capturing the data from an opening till closing square bracket in a capture group to keep it in the output, or split on ; using an alternation | in the pattern.

The pattern matches:

  • ( Capture group 1 (keep after split)
    • \[[^][]*] Match from [...]
  • ) Close group 1
  • | Or
  • ; Match a semicolon

Example

import re

val = "L=1nH;Tol=0.3nH;Idc=1000mA;Isat=NA;Iacpp=NA;Fr=10GHz;ESR=0.10ohm;Q=[100MegHz 7; 500MegHz 23;1.8GHz 47;2.4GHz 57];dL@Isat=NA;dT@Idc=NA"
pattern = r"(\[[^][]*])|;"
result = [s for s in  re.split(pattern, val) if s]
print(result)

Output

['L=1nH', 'Tol=0.3nH', 'Idc=1000mA', 'Isat=NA', 'Iacpp=NA', 'Fr=10GHz', 'ESR=0.10ohm', 'Q=', '[100MegHz 7; 500MegHz 23;1.8GHz 47;2.4GHz 57]', 'dL@Isat=NA', 'dT@Idc=NA']
  • Related