Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

ruby

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

like image 860
ben Avatar asked Oct 12 '10 09:10

ben


3 Answers

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.)

like image 56
Colin Hebert Avatar answered Oct 24 '22 11:10

Colin Hebert


Something like

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

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

like image 22
The Archetypal Paul Avatar answered Oct 24 '22 11:10

The Archetypal Paul


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
like image 2
ghostdog74 Avatar answered Oct 24 '22 13:10

ghostdog74