0

I am working with python 2.7 on Linux. I prepare a regular expression which should detect that a string is in the following format:

tcp:IP_ADDR:PORT-IP_ADDR:PORT or udp:IP_ADDR:PORT-IP_ADDR:PORT

I have the following short script for regular expression:

import re
my_str="tcp:192.168.0.1:111-10.0.0.2:22"
regx_ip = r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
regx_hostfwd = r'["tcp" | "udp"]?:%s?:\d+-%s?:\d+' % (regx_ip, regx_ip)
if not re.match(regx_hostfwd, my_str):  
    print("fail")
else:
    print("success!")

When I run it I get: "fail" However, when I change it to use re.search, thus: ...

my_str="tcp:192.168.0.1:111-10.0.0.2:22"
regx_ip = r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
regx_hostfwd = r'["tcp" | "udp"]?:%s?:\d+-%s?:\d+' % (regx_ip, regx_ip)
if not re.search(regx_hostfwd, my_str): 
    print("fail")
else:
    print("success!")   

...

and run it, I get "success!". I don't understand what is the reason for this. According to my understanding, match looks from the start of the input while search looks anywhere in the input. According to my understanding, in this case it should return success in both cases. What should I do so it will return success also when using "match"?

Regards, Kevin

Kevin Wilson
  • 153
  • 1
  • 5
  • Your regular expression is not correct. print(re.search(regx_hostfwd, my_str)) -> <_sre.SRE_Match object; span=(2, 31), match='p:192.168.0.1:111-10.0.0.2:22'> The documentation explains what is the difference between re.search & re.match https://docs.python.org/2/library/re.html – Victor Feb 09 '19 at 11:02
  • Thanks, Reese, but I am not sure about your answer. When I try print(re.search(regx_hostfwd, my_str)) I get: _sre.SRE_Match object at 0x7f72cb467718>, and when I try print(re.match(regx_hostfwd, my_str)) I get : "None". I tried it with python 2.7. So it seems that match() fails while search() does succeed, exactly as I said before. My question is: what should I do so that match will succeed ? – Kevin Wilson Feb 09 '19 at 11:54
  • Effectively, Python 3.7 shows more details that you can get with Python 2.7. The problem is just that your regex is wrong. I changed regx_hostfwd = r'["abc" |... And it return me success (with re.search) whereas my_str starts with "tcp". Don't forget that the difference between search and match is : search looks for for the first location where the regular expression pattern produces a match whereas match looks at the beginning of string. The problem is just your regular expression. Try for testing your regex online : https://pythex.org/ very useful. – Victor Feb 09 '19 at 19:47
  • The problem is how you created your group. ["tcp"]|["udp"]? means match one letter among ",t,c,p or ",u,d,p. This could be a solution : (tcp|udp):\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}?:\d+-\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}?:\d+ – Victor Feb 09 '19 at 19:55

0 Answers0