-1

Given the following string:

dpkg.log.looker.test.2019-09-25

I'd want to be able to extract:

looker.test or

looker.


I have been trying multiple combinations but none that actually extract only the hostname. If I try to filter the whole beggining of the file (dpkg.log.), it also ignores the subsequent characters:
/[^dpkg.log].+(?=.[0-9]{4}-[0-9]{2}-[0-9]{2})/

returns:
er.test

Is there a way to ignore the whole string "dpkg.log" without ignoring the subsequent repeated characters?

Rafael Oliveira
  • 309
  • 2
  • 11
  • 2
    `s.split('.')[2]`? Should be enough if you need `looker`. Otherwise, what is the right hand boundary criterion? – Wiktor Stribiżew Oct 08 '19 at 19:25
  • 1
    If you mean that matching should stop at the date, try `^dpkg\.log\.(.*?)\.\d{2}(?:\d{2})?-\d{1,2}-\d{1,2}$`. See [this demo](https://regex101.com/r/MVZy3b/1). This is a bit verbose though. – Wiktor Stribiżew Oct 08 '19 at 19:28
  • 1
    FWIW `[^dpkg.log]` is simply ensuring that character at that position doesn't match any of the characters in `dgklop.` (not the string `dpkg.log` - it means any character not in the set `[^.......]`) – ctwheels Oct 08 '19 at 19:29
  • Ok, looks like a classic string between two strings extraction issue. Use a capturing group and get the value via `match.group(1)`. – Wiktor Stribiżew Oct 08 '19 at 19:32
  • The answer given by @Wiktor Stribiżew is accurate. Thank you. The following regex will match anything after "dpkg.log." and before the date at the end of the file: `^dpkg\.log\.(.*?)\.\d{2}(?:\d{2})?-\d{1,2}-\d{1,2}$` – Rafael Oliveira Oct 08 '19 at 19:39

1 Answers1

0

Maybe, the following expression would be working just OK with re.findall:

[^.]+\.[^.]+\.(.+)\.\d{2,4}-\d{2}-\d{2}

Demo

Test

import re

regex = r'[^.]+\.[^.]+\.(.+)\.\d{2,4}-\d{2}-\d{2}'
string = '''
dpkg.log.looker.test.2019-09-25
dpkg.log.looker.test1.test2.2019-09-25
'''

print(re.findall(regex, string))

Output

['looker.test', 'looker.test1.test2']
Community
  • 1
  • 1
Emma
  • 27,428
  • 11
  • 44
  • 69