regex

Hello,

str=~/str1/ && str=~/str2/ && str=~/str3/ ... && str=~/strN/ ?
Is there a method for that like str.match_all(re1,re2,...);
str.include_all(str1,str2,...) or a shorter form?

Another solution using metaprogramming?

thanks
Andrew

I would use Enumerable#all? For this:

[/str1/, /str2, /str3/, …, /strN/].all? { |pattern| str.match?(pattern) }

Regards,
Tom.

···

On 16 Jun 2020, at 11:44, Die Optimisten <inform@die-optimisten.net> wrote:

Hello,

str=~/str1/ && str=~/str2/ && str=~/str3/ ... && str=~/strN/ ?
Is there a method for that like str.match_all(re1,re2,...);
str.include_all(str1,str2,...) or a shorter form?

Another solution using metaprogramming?

thanks
Andrew

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

You're already using regexes. You want *three* problems? :wink:

···

On Tue, Jun 16, 2020 at 6:45 AM Die Optimisten <inform@die-optimisten.net> wrote:

Another solution using metaprogramming?

--
Dave Aronson, T. Rex
Codosaurus, LLC (http://www.Codosaur.us)
Software Development Consulting
How can we evolve you today?

Why three problems ? :slight_smile:

1) Regex per se?

2)

3)

···

Am 6/16/20 um 2:18 PM schrieb Dave Aronson:

On Tue, Jun 16, 2020 at 6:45 AM Die Optimisten > <inform@die-optimisten.net> wrote:

Another solution using metaprogramming?

You're already using regexes. You want *three* problems? :wink:

···

On Jun 16, 2020, at 11:04 AM, Die Optimisten <inform@die-optimisten.net> wrote:

Am 6/16/20 um 2:18 PM schrieb Dave Aronson:

On Tue, Jun 16, 2020 at 6:45 AM Die Optimisten >> <inform@die-optimisten.net> wrote:

Another solution using metaprogramming?

You're already using regexes. You want *three* problems? :wink:

Why three problems ? :slight_smile:

1) Regex per se?

2)

3)

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Not exactly what you’re asking for, but worth noting: "str =~ RegExp.union(/re1/, /re2/, …)" is equivalent to "str =~ /re1/ || str =~ /re2/ …”, that is, the same as you require except for logical or-ing of result instead of and-ing. My understanding that this is much faster than either coding by hand or using ".all?”.

···

On Jun 16, 2020, at 8:15 AM, Walter Lee Davis <waltd@wdstudio.com> wrote:

https://xkcd.com/1171/

On Jun 16, 2020, at 11:04 AM, Die Optimisten <inform@die-optimisten.net> wrote:

Am 6/16/20 um 2:18 PM schrieb Dave Aronson:

On Tue, Jun 16, 2020 at 6:45 AM Die Optimisten >>> <inform@die-optimisten.net> wrote:

Another solution using metaprogramming?

You're already using regexes. You want *three* problems? :wink:

Why three problems ? :slight_smile:

1) Regex per se?

2)

3)

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

love it :slight_smile:

···

On Tue, Jun 16, 2020 at 8:16 AM Walter Lee Davis <waltd@wdstudio.com> wrote:

https://xkcd.com/1171/

> On Jun 16, 2020, at 11:04 AM, Die Optimisten <inform@die-optimisten.net> > wrote:
>
> Am 6/16/20 um 2:18 PM schrieb Dave Aronson:
>> On Tue, Jun 16, 2020 at 6:45 AM Die Optimisten > >> <inform@die-optimisten.net> wrote:
>>
>>> Another solution using metaprogramming?
>> You're already using regexes. You want *three* problems? :wink:
>
> Why three problems ? :slight_smile:
>
> 1) Regex per se?
>
> 2)
>
> 3)
>
>
>
> Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org
?subject=unsubscribe>
> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

--
A musician must make music, an artist must paint, a poet must write, if he
is to be ultimately at peace with himself.
- Abraham Maslow

... that is, the same as you require except for logical or-ing of result instead of and-ing. My understanding that this is much faster than either coding by hand or using ".all?”.

So shouldn't there be an equivalent (fast) method for '&&' ?

'||' should never have precendence over '&&' ;))

Andrew

···

Am 6/17/20 um 12:50 AM schrieb Jack Royal-Gordon:

On Jun 16, 2020, at 8:15 AM, Walter Lee Davis <waltd@wdstudio.com> wrote:

https://xkcd.com/1171/

On Jun 16, 2020, at 11:04 AM, Die Optimisten <inform@die-optimisten.net> wrote:

Am 6/16/20 um 2:18 PM schrieb Dave Aronson:

On Tue, Jun 16, 2020 at 6:45 AM Die Optimisten >>>> <inform@die-optimisten.net> wrote:

Another solution using metaprogramming?

You're already using regexes. You want *three* problems? :wink:

Why three problems ? :slight_smile:

1) Regex per se?

2)

3)

If you're only matching with strings you can dispense with Regexp altogether:

['str1', 'str2', 'str3'].each {|s| 'xxxstr1xxxstr2xxxstr3'.include? s}

gvim

···

On 16/06/2020 11:44, Die Optimisten wrote:

Hello,

str=~/str1/ && str=~/str2/ && str=~/str3/ ... && str=~/strN/ ?
Is there a method for that like str.match_all(re1,re2,...);
str.include_all(str1,str2,...) or a shorter form?

Another solution using metaprogramming?

thanks
Andrew

If you’re designing the language from a “completeness” approach, rather than addressing a particular need, you would be correct. But it seems to me that there are many more usage cases for ORing of regex tests rather than for ANDing them.

···

On Jun 17, 2020, at 12:52 AM, Die Optimisten <inform@die-optimisten.net> wrote:

Am 6/17/20 um 12:50 AM schrieb Jack Royal-Gordon:

... that is, the same as you require except for logical or-ing of result instead of and-ing. My understanding that this is much faster than either coding by hand or using ".all?”.

So shouldn't there be an equivalent (fast) method for '&&' ?

'||' should never have precendence over '&&' ;))

Andrew

On Jun 16, 2020, at 8:15 AM, Walter Lee Davis <waltd@wdstudio.com> wrote:

https://xkcd.com/1171/

On Jun 16, 2020, at 11:04 AM, Die Optimisten <inform@die-optimisten.net> wrote:

Am 6/16/20 um 2:18 PM schrieb Dave Aronson:

On Tue, Jun 16, 2020 at 6:45 AM Die Optimisten >>>>> <inform@die-optimisten.net> wrote:

Another solution using metaprogramming?

You're already using regexes. You want *three* problems? :wink:

Why three problems ? :slight_smile:

1) Regex per se?

2)

3)

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

"Some people, when confronted with a problem, think “I know, I'll use
regular expressions.” Now they have two problems."

http://regex.info/blog/2006-09-15/247 has a good write up on the history of
the quote.

martin

···

On Tue, Jun 16, 2020 at 8:05 AM Die Optimisten <inform@die-optimisten.net> wrote:

Am 6/16/20 um 2:18 PM schrieb Dave Aronson:
> On Tue, Jun 16, 2020 at 6:45 AM Die Optimisten > > <inform@die-optimisten.net> wrote:
>
>> Another solution using metaprogramming?
> You're already using regexes. You want *three* problems? :wink:

Why three problems ? :slight_smile:

1) Regex per se?

2)

3)

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

... that is, the same as you require except for logical or-ing of
result instead of and-ing. My understanding that [Regexp.union] is

much faster

than either coding by hand or using ".all?”.

So shouldn't there be an equivalent (fast) method for '&&' ?

What Regexp.union does is basically to create a new regex by joining the
given regexen with "|" (in a flag-aware way). So Regexp.union(/a/, /b/)
would return a regex equivalent to /a|b/. (Note that Regexp.union is
only actually useful when at least some of the arguments are variables -
in the given example you could just write `/a|b/` without any need for
Regexp.union).

Now the reason that there is no equivalent Regexp.intersection is
that regular expressions have no equivalent & operator, so you can't
just join the regexen like Regexp.union does. And the reason that
there is no & operator is that regex intersection is a fundamentally
more complicated and expensive operation than union. So implementing
& would take a lot of effort and using it could both lead to large
compilation times for your regular expressions as well as the resulting
compiled regex being slow. Therefore implementing intersection for
regular expressions is generally seen as a bad idea for general purpose
regex engines.

···

On 17.06.20 09:52, Die Optimisten wrote:

Am 6/17/20 um 12:50 AM schrieb Jack Royal-Gordon:

ANDing is often not possible or not useful. Consider matching /^foo/ and
/^bar/ - this will never yield true. If you have /^foo/ and /bar$/ then you
can easily combine into /^foo.*bar$/ and for /foo/ and /bar/ you need
/foo.*bar|bar.*foo/ - even worse if there are overlaps in the patterns. For
other combinations of patterns other combination mechanisms might work. The
situation for AND is very different than that for OR.

Cheers

robert

···

On Wed, Jun 17, 2020 at 7:59 PM Jack Royal-Gordon <jackrg@pobox.com> wrote:

If you’re designing the language from a “completeness” approach, rather
than addressing a particular need, you would be correct. But it seems to me
that there are many more usage cases for ORing of regex tests rather than
for ANDing them.

--
[guy, jim, charlie, sho].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/