2

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
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Raees Khan
  • 371
  • 1
  • 5
  • 20

4 Answers4

12

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")
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Forketyfork
  • 7,416
  • 1
  • 26
  • 33
0

Maybe you will be able to do this with regex

>>> import re
>>> CHESS_BOARD_PATTERN = r'http://localhost:13555/ChessBoard_x\d+_y\d+.bmp'
>>> def is_chess_board_endpoint(endpoint):
...     return bool(re.match(CHESS_BOARD_PATTERN, endpoint))
... 
>>> is_chess_board_endpoint('http://localhost:13555/ChessBoard_x16_y20.bmp')
True
>>> is_chess_board_endpoint('http://localhost:13555/ChessBoard_x05_y12.bmp')
True
>>> is_chess_board_endpoint('http://google.com.br')
False

But it is obvious that depending on your solution, you have to improve this regex, because if you change the host (from localhost to 192.168.0.10 for example) will not work.

drgarcia1986
  • 343
  • 1
  • 5
-1
def checkstrings(string1, string2):
    if len(string1) != len(string2):
        return False
    for x in range(len(string1)):
        if x == 35 or x == 36 or x == 39 or x == 40:
            continue
        if string1[x] != string2[x]:
            return False
    return True

JAVA:

public static boolean checkStrings(String string1, String string2){
        if(string1.length() != string2.length()){
                return false;
        }
        for(int x = 0; x < string1.length(); x++){
                if(x == 35 || x == 36 || x == 39 || x == 40){
                        continue;
                }

                if(string1.charAt(x) != string2.charAt(x)){
                        return false;
                }
        }
        return true;

}
rassa45
  • 3,482
  • 1
  • 29
  • 43
  • They don't mean the 16th and 20th characters, they mean the two values. Also, don't compare strings by identity (`is`), use equality (`==`). – jonrsharpe May 31 '15 at 20:15
  • Comparing strings by equality only checks if either one of them is a string. I tried that before in Python and it messed up sometimes – rassa45 May 31 '15 at 20:19
  • 1
    *"Comparing strings by equality only checks if either one of them is a string"* - that is completely untrue. – jonrsharpe May 31 '15 at 20:22
  • Thanks jonrsharpe, I'll keep that in mind. Sorry, I'm also a beginner :P – rassa45 May 31 '15 at 20:28
  • I also have a question, should I get the entire next integer at that position since it could be of different length? – rassa45 May 31 '15 at 20:30
-2

You can loop through characters in the string and check if they are digits or not. I.e.

if x.isdigit():