Home > Software design >  In python how to print a character before a matching pattern that occur multiple times?
In python how to print a character before a matching pattern that occur multiple times?

Time:11-08

I have a stringified data from which I'm trying to print a character that occurs before a matching pattern. This matching pattern will occur multiple times so the result can also be a list of characters

E.g

Stringified data is [[1, "[{\"name\": \"john\", \"id\": \"1\"}]", [2, "[{\"name\": \"john\", \"id\": \"1\"}]"]

The matching pattern from the data will be , "[

The Expected result is 1 2

As we can see the charecter 1 and 2 is printed before each occurance of , "[

CodePudding user response:

Purely on the basis of what's asked in the question (and not wondering WHY?) one could do this:

astring = '[[1, "[{\"name\": \"john\", \"id\": \"1\"}]", [2, "[{\"name\": \"john\", \"id\": \"1\"}]"]'

pattern = ', "['

offset = 0
pchars = []

while (index := astring[offset:].find(pattern)) >= 0:
    if offset   index > 0:
        pchars.append(astring[offset index-1])
    offset  = index   1

print(*pchars)

Output:

1 2

CodePudding user response:

Let me assume that the data you posted is JSON (because it looks like JSON) and you just made a mistake when posting the data here, because the number of opening and closing brackets does not match.

In that case, please do not use Regex at all. Use JSON instead:

import json
data = '[[1, "[{\\"name\\": \\"john\\", \\"id\\": \\"1\\"}]"], [2, "[{\\"name\\": \\"john\\", \\"id\\": \\"1\\"}]"]]'
parsed = json.loads(data)
for arr in parsed:
    print(arr[0], end=" ")

Why? Because JSON can be formatted in different ways. It may be pretty printed, it may have additional spaces. Building a Regex which matches all the possible formats is probably harder that expected.

  • Related