Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a string with key-value pairs if underscore in key

I've a string that has key value pairs with _ as their delimiter. The Problem is, there can be underscores within keys too.

What regex can I use to split these into key value pairs?

Intput:

C_OPS_CITY=SFO_C_OPS_SITE_INDICATOR=Office_IDENTITYCATEGORY=Employee_C_OPS_COMPANY=My Company_

Expected Output:

  • C_OPS_CITY=SFO
  • C_OPS_SITE_INDICATOR=Office
  • IDENTITYCATEGORY=Employee
  • C_OPS_COMPANY=My Company

If not regex, what other logic can I use to split this string into array?

like image 555
Sudhik Avatar asked Dec 01 '25 19:12

Sudhik


2 Answers

The values can be unambiguously identified only if they do not contain underscores. If that's indeed the case for you, you can then use a character set that excludes underscores to match values:

[^=_][^=]*=[^_]+

Demo: https://regex101.com/r/7W1WmX/4

like image 60
blhsing Avatar answered Dec 04 '25 08:12

blhsing


here is something I Conjured up, its not the most elegant code but it does the trick so far:

my_str ="C_OPS_CITY=SFO_C_OPS_SITE_INDICATOR=Office_IDENTITYCATEGORY=Employee_C_OPS_COMPANY=My Company_"
response = my_str.split("=")
for x in range(1,response.__len__()):
   split = response[x].split("_", 1)
   response[x-1] = [response[x-1], split[0]]
   response[x] = split[1]

print(response)

The above code produces the result: [['C_OPS_CITY', 'SFO'], ['C_OPS_SITE_INDICATOR', 'Office'], ['IDENTITYCATEGORY', 'Employee'], ['C_OPS_COMPANY', 'My Company'], '']

The Idea is that we first split by the equal signs as we know those will always exist to delimit the keys and values and then we simply split the values on the first underscore ('_') that we see, any text after that underscore is a key for the next term and we go through each element doing this.

(EDIT) Also I noticed that I just used python without knowing which language you were using but this solution should be easily reproducible in other languages albeit with minor adjustments.

like image 23
TinTanSan Avatar answered Dec 04 '25 09:12

TinTanSan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!