This will match even start of string or end of string with the number. The first group (^|\s)
looks for the start of line or a space characters (equivalent to [\t\n\r\f]
).
Similarly, the last group ($|\s)
looks for the end of line or a space character.
If you need it to match a space character strictly, then replace the \s
with the space character
.
ticket=1740
text=[]
text.append("SNMPD_TRAP_COLD_START:SNMP trap:(17405.737)cold start")
text.append("SNMPD_TRAP_WARM_START:SNMP trap:(4.1740;543;544) warm start")
text.append("SNMPD_TRAP_WARM_START:SNMP trap:( 1740 543;544) warm start")
text.append("1740 SNMPD_TRAP_COLD_START:SNMP trap:(17405.737)cold start")
text.append("SNMPD_TRAP_COLD_START:SNMP trap:(17405.737)cold start 1740")
def find_text(search,input):
import re
REGEX=r'(^|\s)'+str(search)+'($|\s)'
matchObj=re.search(REGEX,input)
if matchObj:
print(input)
else:
print("No match")
for line in text:
find_text(ticket, line)
The result:
No match
No match
SNMPD_TRAP_WARM_START:SNMP trap:( 1740 543;544) warm start
1740 SNMPD_TRAP_COLD_START:SNMP trap:(17405.737)cold start
SNMPD_TRAP_COLD_START:SNMP trap:(17405.737)cold start 1740