Pattern match to fail if two periods in a row

Discussion in 'Ruby' started by Ralph Shnelvar, Nov 26, 2009.

  1. 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?
    Ralph Shnelvar, Nov 26, 2009
    #1
    1. Advertising

  2. On Thu, Nov 26, 2009 at 8:51 PM, Ralph Shnelvar <> wrote:
    > Ok ... I really tried. =A0And I used http://rubular.com/ =A0(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 per=

    iods
    > (e.g. "..") in a row?


    >> "abc..def" !~ /\.\./

    =3D> false

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

    Jesus.
    Jesús Gabriel y Galán, Nov 26, 2009
    #2
    1. Advertising

  3. [Note: parts of this message were removed to make it a legal post.]

    if ! /\.{2,}/

    or something

    2009/11/26 Ralph Shnelvar <>

    > 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?
    >
    >
    >
    Louis-Philippe, Nov 26, 2009
    #3

  4. >>> "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 http://en.wikipedia.org/wiki/E-mail_address
    :with => ????,
    :message => 'invalid email format'
    Ralph Shnelvar, Nov 26, 2009
    #4
  5. [Note: parts of this message were removed to make it a legal post.]

    so then only trying to match:

    /\w+\.?\w*/

    should do

    2009/11/26 Ralph Shnelvar <>

    >
    > >>> "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 http://en.wikipedia.org/wiki/E-mail_address
    > :with => ????,
    > :message => 'invalid email format'
    >
    >
    >
    >
    >
    >
    Louis-Philippe, Nov 26, 2009
    #5
  6. On 11/26/2009 09:46 PM, Ralph Shnelvar wrote:
    >>>> "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 http://en.wikipedia.org/wiki/E-mail_address
    > :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

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Nov 27, 2009
    #6
  7. Ralph Shnelvar 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? :)
    --
    Posted via http://www.ruby-forum.com/.
    Aldric Giacomoni, Nov 27, 2009
    #7
  8. 2009/11/27 Aldric Giacomoni <>:
    > Ralph Shnelvar 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? :)


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

    Kind regards

    robert


    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Nov 27, 2009
    #8
  9. Robert Klemme 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. :(
    --
    Posted via http://www.ruby-forum.com/.
    Aldric Giacomoni, Nov 27, 2009
    #9
  10. Ralph Shnelvar 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
    > http://en.wikipedia.org/wiki/E-mail_address
    > :with => ????,
    > :message => 'invalid email format'


    From http://github.com/alexdunae/validates_email_format_of

    Regex = Regexp.new('^((' + LocalPartUnquoted + ')|(' + LocalPartQuoted +
    ')+)@(((\w+\-+[^_])|(\w+\.[^_]))*([a-z0-9-]{1,63})\.[a-z]{2,6}$)',
    Regexp::EXTENDED | Regexp::IGNORECASE)
    --
    Posted via http://www.ruby-forum.com/.
    Steve Wilhelm, Nov 28, 2009
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wendy Attenberger

    FileOpen Dropping Periods

    Wendy Attenberger, Dec 1, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    340
    Wendy Attenberger
    Dec 1, 2003
  2. Replies:
    2
    Views:
    1,751
    Roedy Green
    Feb 8, 2006
  3. Wenjie

    if (f() != FAIL) or if (FAIL != f())?

    Wenjie, Jul 28, 2003, in forum: C Programming
    Replies:
    3
    Views:
    437
    E. Robert Tisdale
    Jul 31, 2003
  4. D
    Replies:
    0
    Views:
    203
  5. it_says_BALLS_on_your forehead

    qr makes pattern match fail

    it_says_BALLS_on_your forehead, Aug 17, 2006, in forum: Perl Misc
    Replies:
    4
    Views:
    121
    it_says_BALLS_on_your forehead
    Aug 17, 2006
Loading...

Share This Page