Home > Mobile >  remove from 1st upper case alphabetical character to end of string and and special character
remove from 1st upper case alphabetical character to end of string and and special character

Time:03-11

I have a small exercise that required me to remove all special character and the 1st upper case alphabetical character to end of string except dots [.] and hyphens [-] .I tried the solution here https://www.geeksforgeeks.org/remove-uppercase-lowercase-special-numeric-and-non-numeric-characters-from-a-string/
The string below example is one of the example

import re

def removingUpperCaseCharacters(str):
    regex = "[A-Z]"
    return (re.sub(regex, "", str))
def removingSpecialCharacters(str):
 
    # Create a regular expression
    regex = "[^.A-Za-z0-9]"
 
    # Replace every matched pattern
    # with the target string using
    # sub() method
    return (re.sub(regex, "", str))
str = "teachert.memeCon-Leng:"
print("After removing uppercase characters:",
       removingUpperCaseCharacters(str))
print("After removing special characters:",
       removingSpecialCharacters(str))

The output is

After removing uppercase characters: teachert.memeontent-ength:
After removing special characters: teachert.memeContentLength

The ouput I want is

teachert.meme

CodePudding user response:

You could replace one of your functions as follows:

def removingUpperCaseCharacters(str):
    i = 0
    while i < len(str) and (ord('A') > ord(str[i]) or ord('Z') < ord(str[i])):
        i  = 1
    return str[:i]

CodePudding user response:

If I understand correctly, you may do a replacement on [^.A-Za-z0-9] |[A-Z].*$:

str = "teachert.memeCon-Leng:"
output = re.sub(r'[^.A-Za-z0-9] |[A-Z].*$', '', str)
print(output)  # teachert.meme

The above regex pattern will first try to remove special characters. That failing, it will match from the first capital letter until the end of the string.

  • Related