Reputation: 352
I am trying to write a regex to pull all non-comment and non-empty lines from /etc/samba/smb.conf. Comments are lines that:
I tried the following, but it did not properly handle comment type 3.
grep -P '^\s*[^#;]' /etc/samba/smb.conf
This one worked for all 3 types of comments:
grep -P '^\s*[^#;\s]' /etc/samba/smb.conf
Can you explain why adding \s to the character class successfully filtered out comment type 3?
Upvotes: 2
Views: 1975
Reputation: 67968
The problem here is partial matches as you have not used an end anchor $
.
In case of example 3
;
There will be partial matching upto ;
done by \s*
.In the other regex you have disabled \s
so it will not capture the space and partial match is disabled.
The correct regex here is
(?m)^(?!\s*[#;]).+$
Upvotes: 1
Reputation: 41934
[^...]
means does not match any of the characters given at the place of ...
.
You need: ^\s*[#;]
.
Upvotes: 1