0

I am trying to write a quick script to beautify a passwd file for a hack the box vm I am working on. I've run into trouble because the regex grabs more than I want it to.

I am using this as my regex pattern:

passwd_line_regex = re.compile(r'[a-zA-Z0-9_]+:[a-zA-Z0-9:*&_/\ ]+[/-][a-zA-Z0-9_]+\s{1}')

Then I am passing it this (smaller excerpt):

root:*:0:0:Charlie &:/root:/bin/csh toor:*:0:0:Bourne-again Superuser:.....

and using findall I get back a list, but instead of getting root::0:0:Charlie &:/root:/bin/csh AND toor::0:0:Bourne-again, I get them back together like this:

['root:*:0:0:Charlie &:/root:/bin/csh toor:*:0:0:Bourne-again ', 'Superuser:/....

How can I stop this behavior? I want it to stop at the first occurence of (/ or -)example(space), in this case, at the end of /bin/csh, but I am trying to find all entries, so I need to continue from that point with the next entry.

Shelby M.
  • 1
  • 1
  • 1
    Use `re.search` to get the first occurrence. – Wiktor Stribiżew Aug 20 '18 at 19:11
  • It might help if you show a couple of examples of the exact output you're expecting. – nicholishen Aug 20 '18 at 19:24
  • Why do you use `findall` if you don't want to find *all* occurrences? – Barmar Aug 20 '18 at 19:43
  • I think your post was flagged because your question had poorly formatted examples. Next time make sure you specify the exact results you're expecting and be more clear about what your goal is. That being said, I think I understand what you're trying do in spite of the aforementioned. Is this what you needed? https://repl.it/@aalskdjh/LateGrowlingWorkers – nicholishen Aug 20 '18 at 19:46
  • I am using findall because I am trying to find all occurences of the pattern, but I am trying to get the pattern to finish at the first occurrence of /csh(space), and then make a new list item for the next and so on and so on. In this case, it is skipping /csh and continuing to -again(space). – Shelby M. Aug 20 '18 at 22:58

0 Answers0