1

I am trying to match lines in a file that contain only a single / so my thought is i can search for a string of any length that doesn't contain a / and then match exactly one / and then match another string of any length not containing a / and finally ending with a line break.

My attempt at this was [^/]*/[^/]*$. however this doesn't seem to work. I went ahead and tried matching just parts of this pattern and started by just trying to match strings of any length not containing a / which I would think should be just [^/]* but this isn't working.

I am pretty familiar with regex but not as familiar with using it in vim so firstly, am I putting in my regex wrong for using vim? and secondly, if my input for vim is correct, then what is wrong with my regex?

jdodle
  • 141
  • 1
  • 3
  • 11
  • 2
    You need to escape `/` as `\/` and since you want to match with whole line, you also need to have start anchor `^` in the beginning of regex. Try this `^[^\/]*\/[^\/]*$` – Pushpesh Kumar Rajwanshi Feb 18 '19 at 18:34
  • 4
    Possible duplicate of [How to include forward slash in vi search & replace](https://stackoverflow.com/questions/11823616/how-to-include-forward-slash-in-vi-search-replace) – phd Feb 18 '19 at 18:56
  • https://stackoverflow.com/search?q=%5Bvim%5D+search+slash – phd Feb 18 '19 at 18:56
  • This is not a dupe of [How to include forward slash in vi search & replace](https://stackoverflow.com/questions/11823616/how-to-include-forward-slash-in-vi-search-replace) because the `/` chars were not the only problem with the regex. Even if OP knew in that exact command `/` should have been escaped, it wouldn't still work. – Wiktor Stribiżew Feb 19 '19 at 07:42

1 Answers1

3

You may search for all the lines matching your pattern using

:g/^[^\/]*\/[^\/]*$

Note that g will match all occurrences, backslashes need escaping here, and the pattern matches

  • ^ - start of a line
  • [^\/]* - 0+ chars other than /
  • \/ - a /
  • [^\/]* - 0+ chars other than /
  • $ - end of a line.

Note that [^\/]* (negated bracket expression) won't match a line break sequence in Vim, unlike in text editors like Sublime Text 3 or Notepad++, thus, it will match exactly what you need.

Note that you may avoid escaping backslashes if you select another delimiter. See the Vim regex reference:

Frequently you need to do S&R in a text which contains UNIX file paths - text strings with slashes ("/") inside. Because S&R command uses slashes for pattern/replacement separation you have to escape every slash in your pattern, i.e. use "\/" for every "/" in your pattern... To avoid this so-called "backslashitis" you can use different separators in S&R.

So, you may also use :g~^[^/]*/[^/]*$~, or :g#^[^/]*/[^/]*$# as Amadan suggests.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Thanks, I wasn't aware that forward slashes needed to be escaped. I also didn't know what the start of line match notation was so thank you for including that. This is exactly what I was looking for – jdodle Feb 18 '19 at 18:45
  • 1
    Well, slashes _don't_ need to be escaped; _regex delimiters_ do. Now, slashes are the most common regex delimiter, but Vim allows you to use many other things. For example, this is arguably more readable, and shorter, and equally correct: `:g#^[^/]*/[^/]*$#`. – Amadan Feb 18 '19 at 22:43