-3

s = "0|dfgdfg203d{0;15616;5;1651;W;0.0|51651gdgs{0;15616;5;1651;W;0.0|"

I would like to delete whatever is in between "|" and "{" in a string, in this case "dfgdfg203d" and "51651gdgs". The pattern is repetitive.

I've tried to to use some regex but I can't figure out how to do it.

apo02
  • 1
  • 2

1 Answers1

-1

Is this what you are looking for?

>>> import re
>>> s = "0|dfgdfg203d{0;15616;5;1651;W;0.0|51651gdgs{0;15616;5;1651;W;0.0|"
>>> res = re.sub(r'\|[a-zA-Z0-9]{1,}\{','|{',s)
>>> print(res)
0|{0;15616;5;1651;W;0.0|{0;15616;5;1651;W;0.0|
>>>

UPDATED BASED ON COMMENTS

import re

LANGS = [
    'ÁáČčĎďÉéĚěÍíŇňÓóŘřŠšŤťÚúŮůÝýŽž',                               # Czech
    'ÄäÖöÜüẞß',                                                     # German
    'ĄąĆćĘꣳŃńÓ󌜏źŻż',                                           # Polish
    'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZzÅåÄäÖö',   # Swedish
    '°C\\',                                                         # Other necessary characters
    ]

pattern = '\|' + '[A-Za-z{langs}0-9\s]'.format(langs=''.join(LANGS)) + '{1,}\{'


s = ";2;;0| Utlöst rökdeckare {0;0;0;0;;0;A| Servicelarm {0;0;0;0;;0;B| Slinga bruten {0;0;0;0;;0;B| Når e"
res = re.sub(pattern, '|{', s)

print("Input : ", s)
print()
print("Output : ",res)


print("\n\n")

s = "%;0.0| Kompensering ökning °C grader tillopp / °C grader medelrumsavvikelse. {0;3"
res = re.sub(pattern, '|{', s)

print("Input : ", s)
print()
print("Output : ",res)

Output

Input :  ;2;;0| Utlöst rökdeckare {0;0;0;0;;0;A| Servicelarm {0;0;0;0;;0;B| Slinga bruten {0;0;0;0;;0;B| Når e

Output :  ;2;;0|{0;0;0;0;;0;A|{0;0;0;0;;0;B|{0;0;0;0;;0;B| Når e



Input :  %;0.0| Kompensering ökning °C grader tillopp / °C grader medelrumsavvikelse. {0;3

Output :  %;0.0|{0;3
Abhishek Prusty
  • 743
  • 7
  • 14
  • yes, but it doesnt seem to work, maybe it's because there are spaces and tabs in my string sometimes? Here is a example of the real string ";2;;0| Utlöst rökdeckare {0;0;0;0;;0;A| Servicelarm {0;0;0;0;;0;B| Slinga bruten {0;0;0;0;;0;B| Når e" – apo02 Jun 04 '20 at 18:03
  • In that case, the regex would change just a bit. ` res = re.sub(r'\|[a-zA-Z0-9\s]{1,}\{','|{',s)` . The`\s` matches whitespaces. – Abhishek Prusty Jun 04 '20 at 18:07
  • Now its almost there, some are left. This for ex. "%;0.0| Kompensering ökning °C grader tillopp / °C grader medelrumsavvikelse. {0;3" – apo02 Jun 04 '20 at 18:11