Sample string:


Here I need to get for sure: "--"

Same files also contain a lot of similar strings; ------=_, --=_ , -----------, --< which currently also get returned. Therefore the regex must be fit. Currently I also get them :(

Regex I have: ^--\w+ fails in grep, expected results as above, but works in Gedit, Bluefish... grep: grep -r '^--\w+' file .. and returns me no result, and grep -r '^--' file to many of the wrong.

Another one, similar, but more complex: Sample string:


identify this part: --b1_....


Standard grep does not by default understand Perl-like regular expressions (PCRE) such as \w. GNU grep does handle \w (and \W) even in basic and extended regular expressions, which is an extension to the standard behaviour. Other PCRE is enabled in GNU grep using its -P option.

The reason your command returns nothing is that + is an extended regular expression operator, which needs -E to work in GNU grep:

grep -E '^--\w+' file

Without -E, your expression tries to match --W+ literally (where W is some single character matched by \w).

Also note that you may want to anchor the expression at the end as well, as in

grep -E '^--\w+$' file

or else you'll match lines containing non-\w characters later, like


Alternatively, you may use

grep -xE -e '--\w+' file

which does the same thing. The -x option forces a full line match. The -e is needed to delimit the expression from the command line options as the pattern starts with a dash.

GNU grep also understands \+ in basic regular expressions (i.e. when using grep without -E or -P):

grep -x -e '--\w\+' file

Or you could use \{1,\} in place of the \+.

With a non-GNU grep (and GNU grep), you may use [[:alnum:]_] (which matches a letter or a digit, or an underscore which is included separately here) in place of \w:

grep -xE -e '--[[:alnum:]_]+' file

To match hexadecimal numbers and underscores, use [[:xdigit:]_]:

grep -xE -e '--[[:xdigit:]_]+' file


LC_ALL=C grep -xE -e '--[0-9a-fA-F_]+' file

The setting of LC_ALL to C (or to POSIX) for the grep command is necessary since character ranges are locale dependent.

  • See also grep '^--[[:alnum:]_]'. The + is redundant unless you add something to match after that sequence of \ws (like a $ anchor) or use GNU grep's -o (to print what is matched as opposed to the line with the match). – Stéphane Chazelas Mar 11 '19 at 11:43
  • @StéphaneChazelas I think the intention is to anchor the expression at the end of the input string. I will edit and do this earlier than I do now. – Kusalananda Mar 11 '19 at 12:04
  • Use extended regexp
grep -E '^--\w+'
egrep '^--\w+'
  • or escape the repetition-operator
grep '^--\w\+'
  • Note that \w is a PCRE, which is probably the same as [[:alnum:]_] in an extended regular expression. You enable PCRE in GNU grep with -P. – Kusalananda Mar 11 '19 at 9:17
  • This takes me a lot further ;) – Olaf Mar 11 '19 at 9:23
  • @Kusalananda According to re_format(7) extended REs understand those shortcuts too. grep (GNU grep) 2.25 works with \w without need to enable PCRE or extended REs. – Jakub Jindra Mar 11 '19 at 9:27
  • @JakubJindra Yes, it appears GNU grep enables \w as an alias for [_[:alnum:]]. That and \W are the only PCRE it enables by default. – Kusalananda Mar 11 '19 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.