0
import re

list=["22.11","23","66.7f","123abcde","Case44","Happy","78","66.7","yes123","Book111"]

def removeInt(list):
    
for str in list:
            
    if re.match("\d+",str):
       
           result=re.search("\d+",str)
           print("Original sentence:", str)
           print("Found integer",result.group(0),"at the beginning of the string, starting index",result.start(),"and ending index at index",result.end(),".The rest of the string is:"**???????????**)
  

removeInt(list)

Hi, so I'm currently working on a python regex exercise and above is my code for now. in the end of output, if I want to show the rest of string after remove integer from it. What function can I use for it?

here is a sample of output:

Original sentence: “90years” 
Found integer 90 at the beginning of this string, starting at index _ ending at index _. The rest of the string is: years.
5
  • 2
    Just reuse result.end(): print(str[result.end():]) Commented Feb 15, 2021 at 2:05
  • @Chris That will only work if the number is at the start of the string? Assuming OP wants to show the entire string minus the numbers, use re.sub() -- re.sub('\d+', '', '90years') Commented Feb 15, 2021 at 2:07
  • @PacketLoss Since OP uses re.match to filter the ones with number at the beginning, I didn't think it should matter. Commented Feb 15, 2021 at 2:08
  • @Chris After reading more that seems to be the intention, noting resulting print specifies start of string. +1 using the existing slice is a better alternative than re.sub() here. Commented Feb 15, 2021 at 2:11
  • What is your expected result for 22.11? Commented Feb 15, 2021 at 2:49

2 Answers 2

2

You might find this easier using groups to capture the digits and the parts of the string before and after them. Note I've assumed you don't want to match additional digits after the first group:

import re

list=["22.11","23","66.7f","123abcde","Case44","Happy","78","66.7","yes123","Book111"]

def removeInt(list):
    for str in list:
        result = re.match("([^\d]*)(\d+)(.*)",str)
        if result is not None:
            print("Original sentence:", str)
            print("Found integer",
                  result.group(2),
                  "in the string, starting at index",
                  result.start(2),
                  "and ending at index",
                  result.end(2),
                  ". The rest of the string is:", result.group(1)+result.group(3))
  
removeInt(list)

Output:

Original sentence: 22.11
Found integer 22 in the string, starting at index 0 and ending at index 2 . The rest of the string is: .11
Original sentence: 23
Found integer 23 in the string, starting at index 0 and ending at index 2 . The rest of the string is: 
Original sentence: 66.7f
Found integer 66 in the string, starting at index 0 and ending at index 2 . The rest of the string is: .7f
Original sentence: 123abcde
Found integer 123 in the string, starting at index 0 and ending at index 3 . The rest of the string is: abcde
Original sentence: Case44
Found integer 44 in the string, starting at index 4 and ending at index 6 . The rest of the string is: Case
Original sentence: 78
Found integer 78 in the string, starting at index 0 and ending at index 2 . The rest of the string is: 
Original sentence: 66.7
Found integer 66 in the string, starting at index 0 and ending at index 2 . The rest of the string is: .7
Original sentence: yes123
Found integer 123 in the string, starting at index 3 and ending at index 6 . The rest of the string is: yes
Original sentence: Book111
Found integer 111 in the string, starting at index 4 and ending at index 7 . The rest of the string is: Book
Sign up to request clarification or add additional context in comments.

1 Comment

I see! I didn't know that can use groups to capture the digits and the parts of the string before and after them. Thank you!
0

I guess you can use the .find() string method.

The find() method returns the index of first occurrence of the substring (if found). If not found, it returns -1.

The syntax of the find() method is:

str.find(sub, start, end)

where:

  • sub - It is the substring to be searched in the str string.
  • start and end (optional) - The range str[start:end] within which substring is searched.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.