Home > Software design >  Findall specific string and then grab a specific number of characters after string and return both
Findall specific string and then grab a specific number of characters after string and return both

Time:09-22

I am pulling a string that has the same start of characters throughout the string. How do I get that first part but then also grab a certain length after the found string?

String example:

"number='/address/1234'>1234</separator></separator></separator><separator>some more data here</separator><separator>number='/address/5678901234'>5678901234</separator></separator></separator><separator>even more data on this spot</separator><separator>"

Here is what I have to grab at least the first part

re.findall(r"number='/address/", text)

But how do I then just grab a specific amount of characters after. So it returns something like this

[
    'number='/address/1234'>1234</separator></separator></separator><separator>some',
    'number='/address/5678901234'>5678901234</separator></separator></separator><se'
]

So it grabs the first part but then doesnt matter the next part except for length. example above is pulling the first part number='/address/ and then 61 characters after

is this possible with regex in python?

CodePudding user response:

You can use .{61} to select 61 of any character:

re.findall("number='/address/.{61}", text)

In regex . will match any character, and {N} will match whatever came immediately before it N times.

  • Related