-1

I am reading a book on RegEx and going over some of the challenges. I have just started to learn about capturing and non-capturing groups. So, anything above that, I cannot utilize in solving this problem. The person who wrote the book doesn't give you much information on the challenge questions. So, Let me show you the markdown then I will ask my questions:

CHALLENGE Given a list of files in a directory (separated by newlines), identify which files have a vi swap file. vi swap files look like this: .filename.swp. So if there were a file in the directory called .favorite_regexes.txt.swp, you would want to include favorite_regexes.txt in your results.

  • .favorite_regexes.txt.swp
    .practice.py.swp
    .DS_Store
    favorite_regexes.txt
    practice.py
    zippy.py
    
    should match
    • favorite_regexes.txt
    • practice.py
  • file1
    file2
    
    should not match

END CHALLENGE

I am assuming the author is asking me to find any file ending in .swp but match the corresponding .txt file or maybe not? I can't tell.

For instance: I need to find .favorite_regexes.txt.swp but only highlight .favorite_regexes.txt in the list above? I have been trying things like capture groups (.+)(.swp) and non-capture groups (.+)(?:.swp) but it only highlights the entire .swp file. How would I go about finding the .swp file but highlighting the other?

Any help would be much appreciated, Matt

I

Musicman
  • 49
  • 6

1 Answers1

0

So, you wanna match every line that ends with .swp, but only the previous substring. I found this solution called positive positive lookahead. This regex will do: (.*)(?=.swp) If you wanna play with the regex you can do it in regex101.

And this post explains the lookahead pretty well. Be careful: only some languages and browsers support it.

  • I know how to match the filename with your example above . The problem is, I don't think file1 and file2 are supposed to be highlighted. I believe they are supposed to match . But as she says at the end of the challenge.: Do not match file1 or file2. So, I am thinking that it should find the first two examples that end in .swp but only highlight lines 4 and 5. Part of the instructions say do not match the first two files file1, file2. The book hasn't reached the topics of look aheads/behinds, etc... So, I can't use those. – Musicman Jul 13 '21 at 12:44
  • Then again, maybe she is asking what you did? It's hard to tell because they leave out important information. So, I'm just going to assume she meant that. Thanks for working with the solution. – Musicman Jul 13 '21 at 13:01
  • Ok, right. So I see two ways now. One way is to keep going with the lookaheads, and add a lookahead to remove the point at the beginning of the strings. This way, given your input, you get in output the required strings. The output strings would be exactly "favorite_regexes.txt" and "practice.py" and would match the required output. The second way is to do multiple steps. The first step is to search the strings ending with .swp. Once you have the names of the swap files, you can extract substrings and look for exact matches. There are many ways. – Federico Beach Jul 13 '21 at 18:19
  • 1
    It turns out that she was only looking for the following `(.*)(?=.swp)` The look ahead and behind is next chapter. Her description of the problem was terrible. Thank you for your time. – Musicman Jul 13 '21 at 19:16