0

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.

Jawaher
  • 3
  • 2
  • why you removed the `backslashes` function from your question? now your code makes no sense. Anyway, from my understanding what you are seeing is not an error, it is by design. try printing just \x, you will get a syntax error. why do you need to count the backslashes on your URL? What is the purpose. there may be better ways to do it. – Nesi Jul 16 '23 at 10:32
  • Does this answer your question? [How can I put an actual backslash in a string literal (not use it for an escape sequence)?](https://stackoverflow.com/questions/3380484/how-can-i-put-an-actual-backslash-in-a-string-literal-not-use-it-for-an-escape) – slothrop Jul 16 '23 at 10:44

1 Answers1

1

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.

Golil
  • 467
  • 4
  • 12