Search code examples
greppcreregex-lookaroundspositive-lookbehind

Why is PCRE grep treating forward slashes as whitespace?


On bash, I'm trying to get the uppercase letter immediately after a whitespace (A from File A.jpg).

echo "Path/To/File A.jpg" | grep -oP '[A-Z](?!/s)'

This is a negative lookahead (?! that should return any uppercase after a whitespace. So, it should return only A. However, it's returning all upercases:

P
T
F
A

It seems it's treating forward slashes as whitespaces? Why? How can I get only the last A?


Solution

  • should return any uppercase after a whitespace

    No, that’s not what it says at all. It says “any uppercase not followed by forward slash and s.” Well none of them are followed by that. So you get all the uppercase characters.

    What you want is a positive lookbehind.

    (?<=\s)[A-Z]