-->

DEVOPSZONES

  • Recent blogs

    python : regex to remove alphanumeric character from the string at the end

     python : regex to remove alphanumeric character from the string at the end

    Question:

    Regex to remove alphanumeric character from this string "calico-typha-66f59c4ff4" at the end
    in python

    Solution:

    You can use the regular expression module re in Python to remove the alphanumeric characters
    at the end of the string "calico-typha-66f59c4ff4".
    Here is an example of how to do it:


    %%%%%%%%%%%
    import re

    string = "calico-typha-66f59c4ff4"
    regex = r'\w+$' # matches one or more alphanumeric characters at the end of the string
    result = re.sub(regex, '', string)

    print(result) # output: calico-typha-
    %%%%%%%%%%%%%

    In this example, the re.sub() function replaces the matched pattern (\w+$) with an empty
    string, effectively removing the alphanumeric characters from the end of the string.


    Explanation :

        \w : Matches any alphanumeric character; this is equivalent to the                      class [a-zA-Z0-9_].

    $ :  Matches at the end of a line, which is defined as either the end of the string, or any location followed by a newline character.



    No comments