Home > Net >  Python Regex to find all characters between two index values
Python Regex to find all characters between two index values

Time:03-28

Looking for a way to use a Python regex to extract all the characters in a string between to indexes. My code is below:

import re

txt = "Hula hoops are fun."
x = re.search(r"hoops", txt)
c = x.span()
a = c[0]
b = c[1]

print(a) # prints 5
print(b) # prints 10

txt2 = "Hula loops are fun."

z = re.???(a, b, txt2) #<------ Incorrect
print(z)

What I am trying to figure out is to somehow use a and b to get z = "loops" in txt2 (the rewrite of txt). Is there a python regex command to do this?

CodePudding user response:

you can use z = txt[a:b] to extract all characters between a and b indices.

CodePudding user response:

Why not using slices(the obvious way)?

z = txt2[a:b]
print(z)  # loops

If you really want to use regex, you need to consume a . character a times to reach a because Regex doesn't have indexing directly. Then get the next b - a characters. In your case you end up with (?<=.{5}).{5} pattern. (?<=.{5}) part is a positive lookbehind assertion.

pat = rf"(?<=.{{{str(a)}}}).{{{str(b - a)}}}"
print(re.search(pat, txt2))

output:

<re.Match object; span=(5, 10), match='loops'>

CodePudding user response:

import re

txt = "Hula hoops are fun."
x = re.search(r"hoops", txt)
c = x.span()
a = c[0]
b = c[1]

print(a) # prints 5
print(b) # prints 10

txt2 = "Hula loops are fun."
txt3 = list(txt2)
xy = txt3[a:b]
z = ""
for item in xy:
    
    z = z   item

print(z)
  • Related