1

I am using the RegEx search tool in Sublime. I need to select all lines that contain non alphabet characters (excluding spaces).

E.g.

Hedgehog's shoes
The Fox Machine
The Armadillo`s wish

In the above, the entirety of hedgehog and armadillo lines would be selected, because they contain non alphabet characters. The fox line would not be selected.

Here is my code:

.*[^a-zA-Z\s]

Explanation

.* - Selects string
[^] - Ignore everything in the square brackets
a-zA-Z - Ignores all alphabet characters 
\s - Ignores Space

That should work in theory, but it doesn't. It only selects everything up to the offending character, but nothing after it.

Please note, I am looking for a solution that works in Sublime's Find and Replace. If this is not possible in Sublime, then how can I adapt the code so I can search an entire directory of files via the MacOS terminal?

I think doing it in the terminal would be

for f in *; do '.*[^a-zA-Z\s]' 

But that doesn't work either.

PS. There are questions about this on Stackoverflow already (Such as this one). But they only deal with selecting the individual characters and not the whole line.

big_smile
  • 1,487
  • 4
  • 26
  • 59

2 Answers2

2

I suggest using

.*[^[:alpha:]\s].*
.*[^[:alpha:][:space:]].*

Details

  • .* - any zero or more chars other than line break chars, as many as possible
  • [^[:alpha:]\s] / [^[:alpha:][:space:]] - any char but an alphabetic or whitespace char
  • .* - any zero or more chars other than line break chars, as many as possible

See the SublimeText demo:

enter image description here

Note you may use [^[:alpha:][:space:]] in MacOS terminal with grep:

grep '[^[:alpha:][:space:]]' file

See an online demo.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • `Note you may use [^[:alpha:][:space:]] in MacOS terminal with grep:` When I try that on a directory of files, it says "grep: file: No such file or directory" Thanks for any help you can offer! – big_smile Apr 05 '20 at 10:17
  • 1
    `grep '[^[:alpha:][:space:]]' file` will work. If you want to run on some files, you may use `for f in *.your_extension; do grep '[^[:alpha:][:space:]]' "$f"; done`. Instead of `*.your_extension`, you may any other glob pattern to get the files you need. – Wiktor Stribiżew Apr 05 '20 at 10:18
  • 1
    @big_smile To recursively use `grep` to search in the current directory, you may use `grep -R '[^[:alpha:][:space:]]' .`. See [this thread](https://apple.stackexchange.com/questions/275373/how-to-make-grep-work-like-in-ubuntu/275379). Note that in a lot of cases when it comes to using POSIX tools in MacOS, it is recommended to install and use their GNU versions. – Wiktor Stribiżew Apr 05 '20 at 10:23
  • Thanks! One last question, in the terminal, if I want to replace the found line (or delete the line), how would I do that. I thought it would be `for f in *.txt; do grep '[^[:alpha:][:space:]]/d' "$f"; done` with the `/d` standing for the replacement (in this case delete), but it just says `grep: invalid character class` – big_smile Apr 05 '20 at 10:45
  • 1
    @big_smile You do that with `sed`: `for f in *.txt; do sed -i '' '/[^[:alpha:][:space:]]/d' "$f"; done` - note it will remove the lines inline due to `i` option (the`''` is required in a FreeBSD sed). In GNU `sed`, you do not need the `''` after `-i`. – Wiktor Stribiżew Apr 05 '20 at 11:10
1

The pattern you tried first matches until the end of the line and will backtrack to match one of [^a-zA-Z\s]

There is nothing after that pattern so the match will stop there.

As you already have you match, you could match the rest of the line using

.*[^a-zA-Z\s].*
The fourth bird
  • 154,723
  • 16
  • 55
  • 70