I need a regex that will get me in file.txt lines which have something other than just letters or/and numbers and dashes.

file.txt looks like this


But it has like 47k lines :) so it might happen that in file.txt we will have


I would need a regex that will find all lines that have something else than number and/or letters and dashes ideally that I can use with grep :)

grep '[^[:alnum:]-]'

Returns lines that contain any one character that is neither - nor classified as alphanumeric in the locale.

Note that it's not limited to letters of alphabetic scripts, it also includes word constituents of non-alphabetic scripts such as Chinese or Japanese characters. It doesn't include combining diacritics though which means line like Stéphane where the é is expressed as e followed by the U+0301 combining acute accent will be reported.

Note that with many grep implementations, it will fail to report lines that contain sequence of bytes that don't form valid characters in the locale as long as all the valid characters are alnums or -.

grep -vx '[[:alnum:]-]*'

(Replace * (0 or more) with + (1 or more) if you want it to also report empty lines)

Would work better in those cases with some grep implementations. With GNU grep, you may need to add the -a option so it also works even if it detects the input is not valid text.

More generally, portably, you don't get much guarantee with text utilities such as grep when the input is not valid text. That includes sequences of bytes not forming valid characters, but also lines containing the NUL character or overlong lines.

If you want to restrict to the 52 letters and 10 digits of the POSIX portable character set (abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789), you can set the locale to C/POSIX:

LC_ALL=C grep '[^[:alnum:]-]'

Which would also help with character decoding issues as all bytes form one valid (though possibly undefined) character in the C locale.


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.