79459428

Date: 2025-02-22 10:29:20
Score: 2
Natty:
Report link

The simplest way to get # cat and # dog in the example is using -Pattern '^# '

Though, in this case it must be ensured that the line always starts with # followed by at least one whitespace. Lines with leading whitespaces and also strings directly adjacent after # without any whitespaces between will not match.

# cat
######## foo
### bar
#################### fish
# dog
##### test
   #     bird
      #monkey

For getting # cat, # dog, # bird and #monkey it's better to use:

Get-Content file.txt | Select-String -Pattern '^(\s*)#(?!#)'

The solution of using a negative lookahead (?!#) has already been mentioned, don't know why the answer was downvoted.

^(\s*) describes that at the start of the line any whitespace characters in zero or more occurrence before # will match.

Reasons:
  • RegEx Blacklisted phrase (2): downvote
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: burnie