-1


expression to find exact number in string that do not start with dot and not end with any other digit.(description)

ticket=1740
text="SNMPD_TRAP_COLD_START:SNMP trap:(17405.737)cold start"
text2="SNMPD_TRAP_WARM_START:SNMP trap:(4.1740;543;544) warm start"
text3="SNMPD_TRAP_WARM_START:SNMP trap:( 1740 543;544) warm start"

**if re.findall(r'\b'+str(1740)+'(?!\.?\d)', text):
    print(text)
answer should be only text3**
Zaman Azam
  • 39
  • 8

2 Answers2

0

Here is one possible interpretation to your question:

text3 = "SNMPD_TRAP_WARM_START:SNMP trap:( 1740 543;544) warm start"
if re.search(r'^.*(?<![.\d])1740(?![.\d]).*$', text3):
    print("MATCH")
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • I have to print line after finding the Exact number. Not just find the string and print match. Go thorough one by one statement and print line where exacted match with number – Zaman Azam Jun 24 '20 at 03:40
  • @ZamanAzam Then slightly adjust the pattern to also search for `1740` inside the input text. – Tim Biegeleisen Jun 24 '20 at 03:41
  • Tim, are you sure you've interpreted the question correctly? I read it as needing `(?<=\.)1740(?!\d)`. Zamon, do you wish to match every string `1740` for which the `1` is not preceded by a period and the `0` is not followed by a digit? – Cary Swoveland Jun 24 '20 at 03:58
  • @CarySwoveland Read the question `answer should be only text3**` ... I assume that only text3 is expected, because it _does_ contain `1740` but also does _not_ contain any decimal numbers. – Tim Biegeleisen Jun 24 '20 at 03:59
  • Tim, I read the first sentence as, "...expression to find the string `"1740"` that is not preceded by a period or followed by a digit" (though I doubt that the OP wants it preceded by a digit), in part because the OP has `ticket=1740` and the literal `1740` in his/her regex. In other words, I thought the OP is verifying that strings containing `"1740"` have `"1740"` in the right format (and not caring about the rest of the string), and found that only `"text3"` did. This is just so much guesswork, though. Hopefully the OP will clarify. – Cary Swoveland Jun 24 '20 at 04:23
  • @CarySwoveland You know what, you've made me a believer. I edited my question going with your interpretation. – Tim Biegeleisen Jun 24 '20 at 04:27
0

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
kamion
  • 461
  • 2
  • 9