Hi,
I've a multi line string, which I want to delete the first 22 lines from, I then want to use re.findall (which appears to only work on strings).

Is there an easier way to do it than writing the string to a file, reading in as a list, deleting first 22 lines, then writing out and reading in as a string to use re.findall on?

Thanks

Recommended Answers

All 7 Replies

Do re.findall(myre, split(text, 22) [-1])

Hi,

At the moment I have the below, how would I incorporate split, when trying at the moment eclipse complains.

bugtraqtextfinder = re.compile('<title>(.{1,90})</title>')
bugtraqfindtextpat = re.findall(bugtraqtextfinder,bugtraq_str_search)
bugtraqlinkfinder = re.compile('<link>(\S{1,90})</link>')
bugtraqfindlinkpat = re.findall(bugtraqlinkfinder,bugtraq_str_search)
bugtraqdescfinder = re.compile('CDATA\S\s(.{1,90})\s\S\S\S</description>')
bugtraqfinddescpat = re.findall(bugtraqdescfinder,bugtraq_str_search)

Thanks

Tony is going too fast. Let's decompose a little

the_list = the_string.split("\n")
the_list = the_list[22:]
the_string = "\n".join(the_list)
commented: great solution - and explained well. +1

Thank you - works great.

So I understand...

line 1 creates a list based on my multiline string using newline as a split?
line 2 tells it to only include every thing from line 22 onwards?
line 3 - not sure?

THank you

Thank you - works great.

So I understand...

line 1 creates a list based on my multiline string using newline as a split?
line 2 tells it to only include every thing from line 22 onwards?
line 3 - not sure?

THank you

Line 3 joins the list items, inserting newline between the items (the newlines where removed when the string was split at line 1).

Another solution is to find the position of the 22th occurrence of "\n" in the initial string. This could be done with a regex.

Great thank you :D

The 3rd line puts new lines between strngs to replace those splitted. Sorry my code had wrong split syntax as I only quickly texted it from mobile as half pseudo code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.