-2

really sorry for the stupid question, I am not a programmer, just a network admin. Could you please help me with correct re.findall expression to do the following: I have to extract an exact phrase from a text string.

Phrase to Match: " host 192.168.72.2 " (whitespaces matter)

Sample text:

access-list inside extended permit tcp object-group DM_INLINE_NETWORK_555 host 192.168.72.2 object-group DM_INLINE_TCP_35 

As input re.findall gets a variable. If I try to parse something simple, like this:

object network H_Lin_US01
 host 192.168.72.2

this works fine for me:

    kon = []
    kon.append("host "+IP_to_search)
    kon2 = "host "+IP_to_search
    for counter, entry in enumerate(logfile):
        entry=entry[1:] #this cuts whitespace before "host" word
        match = re.findall("%s\s*$" % kon2, entry)
            if match == kon:

It returns exactly "host x.x.x.x " with whitespace in the end.

I need now to return almost the same, but with any prepending text in the beginning + one whitespace before, like " host 1.1.1.1 ". It seems I've tried all possible options here but it still doesn't work as expected. Many many thanks in advance!

  • You do not seem to need `re.findall`. Try `for entry in logfile: if re.search(fr"\bhost\s*{re.escape(IP_to_search)}\b"): print(entry)`. – Wiktor Stribiżew Jul 06 '21 at 09:23
  • Please post the [MCVE (minimal complete verifiable example)](http://stackoverflow.com/help/mcve) to be able to repro the exact issue and showcase a tangible fix. – Wiktor Stribiżew Jul 06 '21 at 09:31

1 Answers1

-1

If you want the whitespace, you could try this pattern to search: \s{1}host\s\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\s{1} if you only want to match a specific IP address, and not ANY IP address, you would just replace the \d{3}\. chain with that IP address, eg: \s{1}host\s{1}123.123.123.123\s{1}.

\s{1} will find only exactly 1 whitespace, \d{1,3} will find any 1-3 combo of digits, and the \. is an escape char used to find the dots separating the digit groups.

EDIT: the above is not a good answer. OP posted in a comment below that "if re.search(fr"\bhost\s*{re.escape(IP_to_search)}\b",entry): works. Please do not mark my answer as correct, just calling attention to OP's solution.

nos codemos
  • 569
  • 1
  • 5
  • 19
  • Note `\s{1}` = `\s`. – Wiktor Stribiżew Jul 06 '21 at 09:25
  • yes, I know, but in the OP (s)he specified only one whitespace, if there is one with more than one whitespace, it would grab all of them, no? i like to make my regex very specific for pattern matching, for peace of mind if nothing else – nos codemos Jul 06 '21 at 09:27
  • `\s` matches only one whitespace. Each unquantified token in a regex pattern matches exactly once. `{1}` makes the pattern confusing and makes the pattern longer. `\d{1-3}` is just wrong. You meant `\d{1,3}`. Where did you take `\d{1-3}` from? Please check [this SO thread](https://stackoverflow.com/questions/3032593). – Wiktor Stribiżew Jul 06 '21 at 09:28
  • It doesn't work for me, sorry, I get: "Traceback (most recent call last): File "decom6.py", line 155, in if re.search(fr"\bhost\s*{re.escape(IP_to_search)}\b"): TypeError: search() missing 1 required positional argument: 'string'" from parser. – KaterHilftKassel Jul 06 '21 at 10:04
  • Is the code correct? print('Membership in ACLs via "host" command:','\n') IP_to_search = "192.168.72.2" kon2 = ' host '+IP_to_search kon = [] kon.append(' host '+IP_to_search) for entry in enumerate(ACLList): if re.search(fr"\bhost\s*{re.escape(IP_to_search)}\b"): print(match) if match == kon: print(entry) input() – KaterHilftKassel Jul 06 '21 at 10:05
  • ah, sorry, i did it from memory which is wrong, replaced the dashes with commas. @KaterHilftKassel the reason you are getting the error is you still have to include the string you want to search as a parameter, based on the error message it looks like you only included the pattern, it should be `re.search(pattern, string)` if memory serves. https://docs.python.org/3/library/re.html. you can use https://regexr.com/ to test patterns as well – nos codemos Jul 06 '21 at 10:06
  • Thanks, much better now. It least now it returns nothing - [] – KaterHilftKassel Jul 06 '21 at 10:15
  • This final code is: print('Membership in ACLs via "host" command:','\n') IP_to_search = "62.190.154.118" kon2 = ' host '+IP_to_search kon = [] kon.append(' host '+IP_to_search) for entry in ACLList: if re.search(fr"\bhost\s*{re.escape(IP_to_search)}\b",entry): print(match) if match == kon: print(entry) input() – KaterHilftKassel Jul 06 '21 at 10:15
  • Gents, this works perfectly well: "if re.search(fr"\bhost\s*{re.escape(IP_to_search)}\b",entry):" – KaterHilftKassel Jul 06 '21 at 10:35