0% found this document useful (0 votes)
4 views

match - regex search in vi for a line that contains a string but does _not_ end with, say, Q - Stack Overflow

match - regex search in vi for a line that contains a string but does _not_ end with, say, Q

Uploaded by

gblackwe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

match - regex search in vi for a line that contains a string but does _not_ end with, say, Q - Stack Overflow

match - regex search in vi for a line that contains a string but does _not_ end with, say, Q

Uploaded by

gblackwe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

match - regex search in vi for a line that contains a string but does *no... https://stackoverflow.com/questions/10229217/regex-search-in-vi-for-...

Just browsing Stack Overflow? Help us improve your experience. Sign up for research

regex search in vi for a line that contains a string but


does *not* end with, say, Q
Asked 12 years, 7 months ago Modified 9 years, 3 months ago Viewed 9k times

Using vi, I want to match a string, but only if the line doesn't end with some letter, say Q.

E.g., if the file is:


1
myQ
my

I want to match the first line but not the second.

From reading related posts it would seem that look aheads should work:

/[?=my][?!Q]

should find just the second line but it finds the first.

regex match

Share Improve this question edited Aug 29, 2015 at 18:49 asked Apr 19, 2012 at 13:33
Follow Tunaki Leo Simon
137k 46 363 433 186 1 9

2 /^.\+[^Q]$/ should do the trick – Michael Wild Apr 19, 2012 at 13:36

@MichaelWild: This should be an answer :) – Tamer Shlash Apr 19, 2012 at 13:48

1 @Mr.TAMER well, the question is too simple ;-) – Michael Wild Apr 19, 2012 at 15:12

2 Answers Sorted by: Highest score (default)

1 of 2 4/12/2024, 7:04 pm
match - regex search in vi for a line that contains a string but does *no... https://stackoverflow.com/questions/10229217/regex-search-in-vi-for-...

I'd go with the following

4 /^.\+[^Q]$^/

Share Improve this answer edited Apr 22, 2012 at 18:57 answered Apr 19, 2012 at 14:46
Follow Thomas Hupkens
1,570 10 17

There's a backslash missing in front of the + operator, otherwise vim interprets it as a literal +
(unless you modify the magic-ness). I couldn't fix it though, because stupid SO wants edit to be a
minimum of six characters long! – Michael Wild Apr 19, 2012 at 15:15

Don't blame SO, blame me for not having reread my answer ;-) Thanks! – Thomas Hupkens Apr 22,
2012 at 18:58

Can't seem to get the edit box to take a linefeed, sorry for the unreadability of this post. Thanks
very much guys for all your responses. I had tried to simplify my question to its essence but I clearly
tried too hard. Suppose my text is CR my CR myQ CR your CR yourQ Then based on what MrTAMER
said, I would expect /^my.\+[Q^]$/ to find just the first line, but it doesn't find anything. I'm also
puzzled by Thomas's suggestion, first since the backslash before the + is now there, and second
because I don't understand the difference between Michael's suggestion and Thomas's
– Leo Simon Apr 23, 2012 at 23:29

Vim has a regex syntax different from Perl's, but we shouldn't need zero-width matches for
your purpose.
0
/my\(.*[^Q]\|\)$

Share Improve this answer Follow answered Mar 24, 2014 at 12:30
Armali
19.3k 14 61 183

2 of 2 4/12/2024, 7:04 pm

You might also like