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 - Stack Overflow
Just browsing Stack Overflow? Help us improve your experience. Sign up for research
Using vi, I want to match a string, but only if the line doesn't end with some letter, say Q.
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
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-...
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