We can also use some advanced regex like in this example
any word preceded by big
grep-P'(?<=big)\s\w+'<<-EOF
big house
small house
big car
small car
EOF
outputs each line that NOT starts with big
grep-P'(?<!big)\s\w+'<<-EOF
big house
small house
big car
small car
EOF
The grep below will catch only lines that start with any word but not followed
by house
grep-P'\w+ (?!house)'<<-EOF
big house
small house
big car
small car
EOF
Now any word followed by house
grep-P'\w+ (?=house)'<<-EOF
big house
small house
big car
small car
EOF
The final example:
I had an html file with some wallpapers "name" on it, and I wanted to print just the names without full path
grep-Poh'(?<=href="/pt-br/wallpapers/details/)[^/]*' file-1.html
-P ...................... Perl regex
-o ...................... only the match
-h ...................... omit the filename
(?<=pattern) .... look behind
[^/]* ................... any character but slash
And an ASCII table about some of these regex patterns
βββββββββββββββ¬βββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ
β Lookaround β Name β What it Does β
βββββββββββββββΌβββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β (?=foo) β Lookahead β word that follows is foo β
β (?<=foo) β Lookbehind β preceding word is foo β
β (?!foo) β Negative Lookahead β word that follows is NOT foo β
β (?<!foo) β Negative Lookbehind β word behind is NOT foo β
βββββββββββββββ΄βββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββ
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
We can also use some advanced regex like in this example
any word preceded by big
outputs each line that NOT starts with big
The grep below will catch only lines that start with any word but not followed
by house
Now any word followed by house
The final example:
I had an html file with some wallpapers "name" on it, and I wanted to print just the names without full path
And an ASCII table about some of these regex patterns