pythonparsingurlcountbackslash

How to Parse the String Backslash "\\" with hex values in Python


I'm trying to parse URLs and count the number of Backslashes. However, in Python, this sign does not parse URLs with hex values. When I use another symbol, such as a slash ('/'), the code works well even with hex numbers. Also, when I alter the hex values to a random string, the code correctly counts the Backslashes. Here's my code:

url = "https://www.example.com/na.g?taetID\x3d5358\1542099"

print(backslashes(url))

#the output>> 0 

#-------
#if I change the hex values to a random string:
#url = "https://www.example.com/na.g?taetID\mnbc\zxcd"

#print(backslashes(url))>> 2 

I want to count the number of Backslashes in all URLs, even those with hex values. In the URL above, there are two backslashes.


Solution

  • your url String is raw String add r in first.

    url = r"https://www.example.com/na.g?taetID\x3d5358\1542099"
    print(url.count("\\"))
    

    output is:

    2
    

    \x3d is ASCII character with the hexadecimal code 3D, is equals sign =. and \154 is octal escape sequence for the ASCII character with the decimal code 108, is l. finally, the expression \x3d5358\1542099 can be interpreted as =5358l2099.