Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to compare two strings ignoring few characters

Tags:

python

I want to compare two strings in python ignoring some characters, like if the string is:

"http://localhost:13555/ChessBoard_x16_y20.bmp"

I want to ignore the values "16" and "20" in the string; no matter what these values are, if the rest of the string is same as this string is then I should get the result "TRUE". How can I do this?

Example:

URL = "http://localhost:13555/ChessBoard_x05_y12.bmp"

if URL == "http://localhost:13555/ChessBoard_x16_y16.bmp":
    print("TRUE")
else:
    print("FALSE")

Output:

TRUE
like image 362
Raees Khan Avatar asked Jan 18 '26 06:01

Raees Khan


1 Answers

Use regular expressions. Here it is for your case. A dot matches any symbol. A \d matches a digit. Some special symbols have to be escaped.

import re
if re.match(r'http://localhost:13555/ChessBoard_x\d\d_y\d\d\.bmp', URL):
    print("TRUE")
else:
    print("FALSE")
like image 188
Sergei Avatar answered Jan 19 '26 20:01

Sergei



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!