Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

85%
+10 −0
Q&A Vim: how to search for all instances of a string, except for those that are between two specific strings

Using Vim, I am trying to search for all instances of a specific string, except for those that fall somewhere in between two other specific strings. For example, I want to determine all instances ...

2 answers  ·  posted 2y ago by Trevor‭  ·  last activity 2y ago by Dirk Herrmann‭

#3: Nominated for promotion by user avatar Alexei‭ · 2022-06-12T05:34:46Z (almost 2 years ago)
#2: Post edited by user avatar Trevor‭ · 2022-06-10T05:08:21Z (almost 2 years ago)
made first sentence a little more specific
  • Using Vim, I am trying to search for all instances of a string, except for those that fall somewhere in between two specific strings.
  • For example, I want to determine all instances of `bird`, except for those that fall somewhere between `abc` and `xyz`. So in the text below, a successful command would find only the 1st and 3rd instances of `bird` on line 1, and only the 1st instance of `bird` on line 2:
  • ```
  • bird ant abc cow bird xyz cat bird
  • cat dog fish bird abc bird horse xyz
  • ```
  • ---
  • **my best efforts:**
  • This search seems to find all instances of `bird` that do not have `xyz` somewhere after them:
  • ```
  • /bird\(.*xyz\)\@!
  • ```
  • This search seems to find all instances of `bird` that do not have `abc` somewhere before them (a similar example is shown in `:help \@<!`)
  • ```
  • /\(abc.*\)\@<!bird
  • ```
  • But naively trying to combine these two patterns does not work:
  • ```
  • /\(abc.*\)\@<!bird\(.*xyz\)\@!
  • ```
  • Using Vim, I am trying to search for all instances of a specific string, except for those that fall somewhere in between two other specific strings.
  • For example, I want to determine all instances of `bird`, except for those that fall somewhere between `abc` and `xyz`. So in the text below, a successful command would find only the 1st and 3rd instances of `bird` on line 1, and only the 1st instance of `bird` on line 2:
  • ```
  • bird ant abc cow bird xyz cat bird
  • cat dog fish bird abc bird horse xyz
  • ```
  • ---
  • **my best efforts:**
  • This search seems to find all instances of `bird` that do not have `xyz` somewhere after them:
  • ```
  • /bird\(.*xyz\)\@!
  • ```
  • This search seems to find all instances of `bird` that do not have `abc` somewhere before them (a similar example is shown in `:help \@<!`)
  • ```
  • /\(abc.*\)\@<!bird
  • ```
  • But naively trying to combine these two patterns does not work:
  • ```
  • /\(abc.*\)\@<!bird\(.*xyz\)\@!
  • ```
#1: Initial revision by user avatar Trevor‭ · 2022-06-10T05:07:27Z (almost 2 years ago)
Vim: how to search for all instances of a string, except for those that are between two specific strings
Using Vim, I am trying to search for all instances of a string, except for those that fall somewhere in between two specific strings.

For example, I want to determine all instances of `bird`, except for those that fall somewhere between `abc` and `xyz`. So in the text below, a successful command would find only the 1st and 3rd instances of `bird` on line 1, and only the 1st instance of `bird` on line 2:
```
bird ant abc cow bird xyz cat bird
cat dog fish bird abc bird horse xyz
```

---
**my best efforts:**

This search seems to find all instances of `bird` that do not have `xyz` somewhere after them:
```
/bird\(.*xyz\)\@!
```
This search seems to find all instances of `bird` that do not have `abc` somewhere before them (a similar example is shown in `:help \@<!`)
```
/\(abc.*\)\@<!bird
```
But naively trying to combine these two patterns does not work:
```
/\(abc.*\)\@<!bird\(.*xyz\)\@!
```