Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regexp exercise 15): solution improvement #7

Open
FabijanC opened this issue Apr 18, 2024 · 4 comments
Open

Regexp exercise 15): solution improvement #7

FabijanC opened this issue Apr 18, 2024 · 4 comments

Comments

@FabijanC
Copy link

FabijanC commented Apr 18, 2024

First of all, thanks for the great tutorial and the CLI app for exercising!

Sorry, I accidentally pressed Submit before finishing my issue. Here's the rest.

In question 21/88, i.e. exercise 15) of the Regular Expressions section, the proposed solution is this:

**15)** For the input file `patterns.txt`, filter lines containing three or more occurrences of `ar` and replace the last but second `ar` with `X`.
```bash
$ awk 'BEGIN{r = @/(.*)ar((.*ar){2})/} $0~r{print gensub(r, "\\1X\\2", 1)}' patterns.txt
par car tX far Cart
pXt cart mart
```

Isn't this a shorter solution:

awk '/(.*ar.*){3,}/{print gensub(/ar/, "X", NF-2)}' patterns.txt

Also, is the phrasing "last but second" correct? I was rather confused. Is the intended meaning the same as that of the word "antepenultimate"?

@FabijanC FabijanC changed the title Solution improvement Regexp exercise 15): solution improvement Apr 18, 2024
@FabijanC
Copy link
Author

FabijanC commented Apr 18, 2024

There were some bugs in the original awk code I provided, they occurred because I had to re-write them here in the issue as I couldn't copy-paste them. The CLI app doesn't allow selecting any content. I see this as an issue - should I open a new one here on GitHub? Or is that inherent to that kind of application?

Anyway, my awk code should now be bug-free and yielding the same output as yours.

@learnbyexample
Copy link
Owner

Your solution won't work for input like par:car tar-far;Cart since you are making use of fields. The question is intended as a search and replace problem. I should probably add such an input line or make the question clearer.

Regarding last-but-second:

par car tar far Cart
                 ^^last
             ^^last but one
         ^^last but two 

Regarding the CLI app, I don't know if there's a way to select content. I could probably add a button/short-cut to copy the content in the Input box, but other than that I doubt I'll be able to add other features. I'm using a third-party Python module for that app and I'm not really familiar with it.

@learnbyexample
Copy link
Owner

Building upon your idea, I think awk -F'ar' 'NF>3{print gensub(FS, "X", NF-3)}' will work. Will update if I can think of case where this'd fail.

@FabijanC
Copy link
Author

Thanks for the reply! About an hour after asking I realized that I was making use of fields, but wasn't at the computer to reply. Anyway, I'm still a newcomer to awk, so I'll try forgiving myself this mistake.

Here's an interesting link regarding "last but second".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants