(?:pattern)
matches pattern
without capturing the contents of the match. It is used with the following *
to allow you to specify zero or more matches of the contents of the ( )
without creating a capture group. This affects the result in python if you used something like re.search()
, as the MatchObject
would not contain the part from the (?: )
. In grep, the result isn't return in the same way, so can just remove the ?:
to use a normal group:
grep -E '[a-z]+(-[a-z]+)*' file
Here I'm using the -E
switch to enable extended regular expression support. This will output each line matching the pattern - you can add the -o
switch to only print the matching parts.
As mentioned in the comments (thanks), it is possible to use back-references (like \1
) with grep to refer to previous capture groups inside the pattern, so technically the behaviour is being changed slightly by removing the ?:
, although this isn't something that you're doing at the moment so it doesn't really matter.