Validate that string contains only allowed characters in Ruby

前端 未结 4 1634
情深已故
情深已故 2021-01-07 12:13

How can I test whether a Ruby string contains only a specific set of characters?

For example, if my set of allowed characters is \"AGHTM\" plus digits <

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-07 12:54

    This seems to be faster than all previous benchmarks (by @Eric Duminil)(ruby 2.4):

    !string.match?(/[^AGHTM0-9]/) 
    

提交回复
热议问题