Pattern match to fail if two periods in a row

Ok … I really tried. And I used http://rubular.com/ (really nice)

And I read and reread the pickaxe book section on regular
expressions.

In other words … I tried.

So …

How does one create an expression that fails if there are two or more
periods
(e.g. “…”) in a row?

On Thu, Nov 26, 2009 at 8:51 PM, Ralph S. [email protected]
wrote:

(e.g. “…”) in a row?

“abc…def” !~ /../
=> false

One easy way is to use the negated match operator (!~). Then the
regexp is trivial.

Jesus.

if ! /.{2,}/

or something

2009/11/26 Ralph S. [email protected]

On 11/26/2009 09:46 PM, Ralph S. wrote:

Basically … I need something that will work in a Rails validates_format_of

Reject if the email address has two periods in a row

validates_format_of :email,
# See Email address - Wikipedia
:with => ???,
:message => ‘invalid email format’

You could try negative lookahead

irb(main):001:0> s=[“aaa”, “a.”, “.a”, “a…”, “…a”]
=> [“aaa”, “a.”, “.a”, “a…”, “…a”]
irb(main):002:0> s.map {|x| [x, /\A(?:.(?!..))*\z/ =~ x]}
=> [[“aaa”, 0], [“a.”, 0], [“.a”, 0], [“a…”, nil], [“…a”, 0]]

Or maybe there is an “:without” which uses the negated match?

Kind regards

robert

“abc…def” !~ /../
=>> false

I don’t think that
“abc…def” !~ /../
is a regular expression.

/../
is a regular expression.

Basically … I need something that will work in a Rails
validates_format_of

Reject if the email address has two periods in a row

validates_format_of :email,
# See

:with => ???,
:message => ‘invalid email format’

Ralph S. wrote:

How does one create an expression that fails if there are two or more
periods
(e.g. “…”) in a row?

As previously mentioned, you can use this:
/.{2,}/
You can also use this:
/[…+]/

Though of course, you -want- to get ‘nil’ instead of a match.

On the other hand, if you are looking to validate an email address,
there are already rails plugins that do that (and more) - so why
reinvent the wheel? :slight_smile:

2009/11/27 Aldric G. [email protected]:

Ralph S. wrote:

How does one create an expression that fails if there are two or more
periods
(e.g. “…”) in a row?

As previously mentioned, you can use this:
/.{2,}/

That only positively matches strings which contain at least two dots
in a row. OP specifically wanted to not have a match for two dots
in a row.

You can also use this:
/[…+]/

Did you test that? How should this expression match two dots in a
row? You have define a character class with “.” and “+” where one dot
is redundant.

irb(main):001:0> /[…+]/ =~ “+”
=> 0
irb(main):002:0> /[…+]/ =~ “.”
=> 0

On the other hand, if you are looking to validate an email address,
there are already rails plugins that do that (and more) - so why
reinvent the wheel? :slight_smile:

If he’s reinventing the wheel then that’s certainly not a good idea.
I just guess that you are talking about a different wheel. :slight_smile:

Kind regards

robert

so then only trying to match:

/\w+.?\w*/

should do

2009/11/26 Ralph S. [email protected]

Ralph S. wrote:

Basically … I need something that will work in a Rails
validates_format_of

Reject if the email address has two periods in a row

validates_format_of :email,
# See
Email address - Wikipedia
:with => ???,
:message => ‘invalid email format’

From GitHub - validates-email-format-of/validates_email_format_of: Validate e-mail addreses against RFC 2822 and RFC 3696 with this Ruby on Rails plugin and gem.

Regex = Regexp.new(‘^((’ + LocalPartUnquoted + ‘)|(’ + LocalPartQuoted +
‘)+)@(((\w+-+[^])|(\w+.[^]))*([a-z0-9-]{1,63}).[a-z]{2,6}$)’,
Regexp::EXTENDED | Regexp::IGNORECASE)

Robert K. wrote:

You can also use this:
/[…+]/

Did you test that? How should this expression match two dots in a
row? You have define a character class with “.” and “+” where one dot
is redundant.

irb(main):001:0> /[…+]/ =~ “+”
=> 0
irb(main):002:0> /[…+]/ =~ “.”
=> 0

I did test it. Just not well. :frowning: