How to read only English characters

后端 未结 3 2026
闹比i
闹比i 2021-01-21 14:23

I am reading a file that sometimes has Chinese and characters of languages other than English.

How can I write a regex that only reads English words/letters?

Sh

3条回答
  •  半阙折子戏
    2021-01-21 14:51

    You need $, which means end of line:

    /^[a-zA-Z]+$/
    

    Or if you use such filtration:

    strings.select { |s| /^[a-zA-Z]+$/ =~ s }
    # which is equal to strings.grep /^[a-zA-Z]+$/
    

    you can use negative filtration method with slightly simplier regex:

    strings.reject { |s| /[^a-zA-Z]/ =~ s }
    

    where [^a-zA-Z] means any non-english character.

提交回复
热议问题