开发者

What is the Ruby regex to match a string with at least one period and no spaces?

开发者 https://www.devze.com 2023-01-19 20:23 出处:网络
What is the regex to match a string with at least one perio开发者_如何学编程d and no spaces?You can use this :

What is the regex to match a string with at least one perio开发者_如何学编程d and no spaces?


You can use this :

/^\S*\.\S*$/

It works like this :

^    <-- Starts with
  \S   <-- Any character but white spaces (notice the upper case) (same as [^ \t\r\n])
  *    <-- Repeated but not mandatory
  \.   <--  A period
  \S   <-- Any character but white spaces
  *    <-- Repeated but not mandatory
$    <-- Ends here

You can replace \S by [^ ] to work strictly with spaces (not with tabs etc.)


Something like

 ^[^ ]*\.[^ ]*$

(match any non-spaces, then a period, then some more non-spaces)


no need regular expression. Keep it simple

>> s="test.txt"
=> "test.txt"
>> s["."] and s.count(" ")<1
=> true
>> s="test with spaces.txt"
=> "test with spaces.txt"
>> s["."] and s.count(" ")<1
=> false


Try this:

/^\S*\.\S*$/
0

精彩评论

暂无评论...
验证码 换一张
取 消