Find and remove everything else
Well, it really is a bit tricky. :-)
If we change my earlier text to this (which is more of what I think you have):
After a weekend of emotional honesty at an Esalen-style retreat, Los Angeles sophisticates /.*Bob*./ and /.*Carol*./ Sanders (Robert Culp and Natalie Wood) return home determined to embrace complete openness. They share their enthusiasm and excitement over their new-found philosophy with their more conservative friends Ted and Alice Henderson (/.*Elliott*./ Gould and Dyan Cannon), who remain doubtful. Soon after, filmmaker Bob has an affair with a young production assistant on a film shoot in San Francisco. When he gets home he admits his liaison to Carol, describing the event as a purely physical act, not an emotional one. To Bob's surprise, Carol is completely accepting of his extramarital behavior. Later, Carol gleefully reveals the affair to /.*Ted*./ and /.*Alice*./ as they are leaving a dinner party. Disturbed by Bob's infidelity and Carol's candor, Alice becomes physically ill on the drive home. She and Ted have a difficult time coping with the news in bed that night. But as time passes they grow to accept that Bob and Carol really are fine with the affair. Later, Ted admits to Bob that he was tempted to have an affair once, but didn't go through with it; Bob tells Ted he should, rationalizing: "You've got the guilt anyway. /.*Don't waste it*./."
If we then try this replacement:
(regular expression search mode)
We’ll (again) obtain:
Bob Carol Elliott Ted Alice Don't waste it
I used the
\Econstructs to avoid leaning-toothpick-syndrome, somewhat.
Terry R last edited by Terry R
Close, but the pattern you have is off…on the end
My solution was:
again a regular expression so search mode is regular expression.
Where (again) leaning toothpicks are all around.
PS I should add there will likely be a last empty line, just a side effect of how the regex works. Should be easy enough to remove that afterwards.
@Alan-Kilborn Rookie mistake… i didnt realize the formatter was gonna make me look like a bonehead.
guy038 last edited by guy038
I know, I’m a bit late :-) Here is my solution !
Assuming that the exact syntax is :
(?s)syntax means that the regex
.char will match any single character, even an
Then , in two parts of the search expression, the regex syntax
.+?represents the shortest non-null range of characters till, either, the strings
Because of the regex symbols
., these characters must be escaped with an slash, so the form
As the second
.+?syntax is embedded between parentheses, the second range of chars ( each keyword ) is stored as group
Finally , then no more keyword exists, the second alternative
.+looks for the greatest non-null range of characters till… the very end of file
In replacement, the conditional structure
?1\1\r\nmeans that if the group
1exists, it is rewritten
\1, followed with a line break
\r\n. When the second alternative of the search occurs, no group is involved. So nothing occurs, and the last range of text, after the last keyword, is simply deleted
Alan Kilborn last edited by
But really, Guy, there isn’t anything new here over what you posted HERE – with the removal of the
^as discussed a bit later in that thread – it’s just an application of the other posting’s idea to slightly different data.
We probably should stop solving the specific problems and just point people to the already-derived general solutions.
guy038 last edited by
Yes, I agree that it looks like a redundant piece of information ! In fact, I was thinking to this old post, where I proposed a general method, for isolating literal strings or expressions matched by a given regex, rewritten on different lines :
That’s the reason why, in my previous post, I preferred to focus on the regexes’s explanations, thinking it could be useful to the OP, anyway !
But, Alan, you’re right : my post wasn’t really needed ;-))
Alan Kilborn last edited by
I had a further thought:
The thread I linked to earlier, and referred to in my post just above is entitled “Marked Text Manipulation”.
That relates to the current thread because a typical desire after marking some text is to copy only that text to another location, which is very similar to the topic of this “Find and remove everything else” thread.
In both cases you obtain the same effective result.
The new thought is that, at the time of the “Marked Text Manipulation” thread’s main discussion, there was no way to copy marked text without resorting to scripting. Now (7.9.1-ish) there is:
Just press the indicated button after you already have marked some text.
I will put a similar not in that other thread as well.
guy038 last edited by
Oh, yes, Alan. You’re right ! Of course, I already downloaded the portable
v7.9.1version but I’m still “stuck” with the
v7.8.5version which explains why I didn"t notice this recent enhancement !
So, thanks to @scott-sumner, we just have to use the
(?-s)/\.\*\K(.+?)(?=\.\*/)regex, click on the Mark All button to get all the keywords and, then, click on the
Copy Marked Textbutton and paste the results on a new document. Nice !