Argo
Argo

Reputation: 103

How can I get some characters around another character in a string?

I have:

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form.

I want to search for 1500s and then select some characters around it, like: ever since the 1500s, when an unknown. Considering that I am searching in a for loop, trying to find all the 1500s in a very long string. So the next loop would be finding: used since the 1500s is reproduced

I am using regular expression to find the substring:

substring = re.findall('1500s', string)

But how to select some 20 characters around it as well?

Upvotes: 1

Views: 277

Answers (2)

Mr Mystery Guest
Mr Mystery Guest

Reputation: 1484

 r"(.{20})?(1500)(.{20})?" g

This will search for "1500" at the start/end of the string as well. I put them in groups just to tidy up the regex.

See it work here

Upvotes: 0

tdube
tdube

Reputation: 2553

As cricket_007 said, you can try using .{20} as bookends to your regular expression pattern.

substring = re.findall('.{0,20}1500s.{0,20}', s)

Upvotes: 2

Related Questions