Stories
Slash Boxes
Comments

SoylentNews is people

posted by on Sunday March 26 2017, @11:59PM   Printer-friendly
from the hooray-for-regex dept.

Ruby 2.4.1 was released this week and included an upgrade to its underlying regular expression engine, Onigmo. The headline feature in this update was support for 'the absent operator' but what is this and what is it for?

An issue on the Onigmo repository about the absent operator pointed to a Japanese academic paper [PDF] that, to my delight, uses Ruby for its examples. Not being a reader of Japanese, I struggled to grasp the concept but it seemed to promise to provide developers with a new mechanism to more easily notate complex matches.

The next step towards an absent operator in Ruby's regular expressions system dates back 5 years to a suggestion for adding a 'negation flag'. It was suggested that a v flag could negate a regular expression. For example, /(?v:ruby)/ would match anything that /ruby/ didn't.

Source: https://medium.com/rubyinside/the-new-absent-operator-in-ruby-s-regular-expressions-7c3ef6cd0b99


Original Submission

 
This discussion has been archived. No new comments can be posted.
Display Options Threshold/Breakthrough Mark All as Read Mark All as Unread
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
  • (Score: 1) by tftp on Monday March 27 2017, @02:31AM (10 children)

    by tftp (806) on Monday March 27 2017, @02:31AM (#484501) Homepage

    I don't know Ruby or Japanese, but in Perl

    For example, /(?v:ruby)/ would match anything that /ruby/ didn't.

    the same thing would roughly be:

    if ($_ =~ m|^(.*)ruby(.*)$|) { $_ = $1 . $2; }

  • (Score: 2) by Whoever on Monday March 27 2017, @02:34AM (1 child)

    by Whoever (4524) on Monday March 27 2017, @02:34AM (#484502) Journal

    Only if there was only one instance of "ruby" in the string.

    • (Score: 1, Funny) by Anonymous Coward on Monday March 27 2017, @02:36AM

      by Anonymous Coward on Monday March 27 2017, @02:36AM (#484505)

      Ah, I see - you want the whole CPAN module right now :-)

  • (Score: 2) by jcross on Monday March 27 2017, @03:26AM

    by jcross (4009) on Monday March 27 2017, @03:26AM (#484510)

    Ah, ruby and perl, the scrapple of computer languages.

  • (Score: 0) by Anonymous Coward on Monday March 27 2017, @03:56AM

    by Anonymous Coward on Monday March 27 2017, @03:56AM (#484518)

    Shouldn't read perl code on a full stomach. Ulp.

  • (Score: 3, Interesting) by Whoever on Monday March 27 2017, @04:24AM (1 child)

    by Whoever (4524) on Monday March 27 2017, @04:24AM (#484521) Journal

    How about this to version which achieves the same objective, but also allows for multiple instances of "ruby":

    s/ruby//g;

    • (Score: 4, Informative) by FatPhil on Monday March 27 2017, @01:06PM

      by FatPhil (863) <{pc-soylent} {at} {asdf.fi}> on Monday March 27 2017, @01:06PM (#484589) Homepage
      You've not understand the objective, partly because context wasn't properly provided. If you think you can solve a problem really trivially, then the chances are you've not understood the problem.

      If it's similar to the issue that perl went through ages back (yay - lets reinvent the wheel!), then the context may well be this: You're seaerching for "ruby up rails", "ruby in rails", "ruby by rails", "ruby plop rails", and everything that's "ruby [something] rails" but isn't "ruby on rails", then you need the "on" to be absent. Simple negations of matches in traditional regexps, such as /ruby [^o][^n] rails/, will never work. Things that would work tend to have unpleasant explosion of complexity: /ruby (.|o[^n]|[^o]n|...+) rails/, which isn't even correct despite superficially being so - more explosion is required, and don't generalise easily to anything apart from single fixed strings.
      --
      Great minds discuss ideas; average minds discuss events; small minds discuss people; the smallest discuss themselves
  • (Score: 1) by Mike on Monday March 27 2017, @04:46AM (1 child)

    by Mike (823) on Monday March 27 2017, @04:46AM (#484523)

    I may not be understanding the issue, but it would likely be simpler in Perl to just do

    ( !~ /ruby/ )

    Not sure why Ruby wouldn't have something like this, but I haven't played with Ruby.

    • (Score: 0) by Anonymous Coward on Monday March 27 2017, @09:29AM

      by Anonymous Coward on Monday March 27 2017, @09:29AM (#484559)

      Well, the article goes into depth why it is not a negation operator. For example, if you don't require a full-string match, matching on "(absent ruby) on rails" does give you a match for "ruby on rails", as e.g. "uby on rails" is a substring that starts with something not matching "ruby" and continues with " on rails".

  • (Score: 3, Informative) by bzipitidoo on Monday March 27 2017, @05:47AM (1 child)

    by bzipitidoo (4388) on Monday March 27 2017, @05:47AM (#484533) Journal

    How about:

    grep -v ruby

    • (Score: 2) by pkrasimirov on Tuesday March 28 2017, @08:32AM

      by pkrasimirov (3358) Subscriber Badge on Tuesday March 28 2017, @08:32AM (#485070)

      That's exactly what they did, up to the 'v'.