f



RCR: regex + regex

Feedback on the following suggestion for ruby:
by default allow for adding regex's

i.e.
>> /foo/ + /bar/
=> /foobar/

Thoughts?
-r
-- 
Posted via http://www.ruby-forum.com/.

0
rogerpack2005 (1307)
10/26/2009 9:13:49 PM
comp.lang.ruby 48886 articles. 0 followers. Post Follow

3 Replies
2202 Views

Similar Articles

[PageSpeed] 16

On Oct 26, 2009, at 5:13 PM, Roger Pack wrote:

> Feedback on the following suggestion for ruby:
> by default allow for adding regex's
>
> i.e.
>>> /foo/ + /bar/
> => /foobar/
>
> Thoughts?
> -r
> --

irb> a=/foo/
=> /foo/
irb> b=/bar/
=> /bar/
irb> class Regexp
irb>   def +(other)
irb>     self.class.new(self.to_s + other.to_s)
irb>     end
irb>   end
=> nil
irb> a+b
=> /(?-mix:foo)(?-mix:bar)/

This is obviously too naive an implementation, but if we change a to / 
foo/i then I'd expect
"Foobar" =~ (a+b)
to be true (well, I mean 0, of course) and
"fooBar" =~ (a+b)
to be nil.

What might be the corresponding * or - behaviors? It seems like
	a * b
is closer to
	/(a)*(b)/
than anything else I could think of and that immediately implies:
	a + b
becomes:
	/(a)+(b)/
rather than just /(a)(b)/

The fact that + is a meaningful character in a Regexp makes a  
universal meaning for it as an operation *on* regexps a bit ambiguous.

-Rob

Rob Biedenharn		http://agileconsultingllc.com
Rob@AgileConsultingLLC.com




0
Rob7461 (595)
10/26/2009 9:52:19 PM
> What might be the corresponding * or - behaviors? It seems like
>   a * b
> is closer to
>   /(a)*(b)/


That's what I'd guess for *, as well.

> than anything else I could think of and that immediately implies:
>   a + b
> becomes:
>   /(a)+(b)/
> rather than just /(a)(b)/
> 
> The fact that + is a meaningful character in a Regexp makes a
> universal meaning for it as an operation *on* regexps a bit ambiguous.

Yeah, or what |
means or what not.  I'd probably just stick with concatenation and not 
even *define* *.

-r
-- 
Posted via http://www.ruby-forum.com/.

0
rogerpack2005 (1307)
10/26/2009 10:08:51 PM
2009/10/26 Roger Pack <rogerpack2005@gmail.com>:
>> What might be the corresponding * or - behaviors? It seems like
>> =A0 a * b
>> is closer to
>> =A0 /(a)*(b)/
>
>
> That's what I'd guess for *, as well.
>
>> than anything else I could think of and that immediately implies:
>> =A0 a + b
>> becomes:
>> =A0 /(a)+(b)/
>> rather than just /(a)(b)/
>>
>> The fact that + is a meaningful character in a Regexp makes a
>> universal meaning for it as an operation *on* regexps a bit ambiguous.
>
> Yeah, or what |
> means or what not. =A0I'd probably just stick with concatenation and not
> even *define* *.

You are aware that you can use string interpolation in regular
expressions, do you?

irb(main):001:0> a=3D/foo/
=3D> /foo/
irb(main):002:0> b=3D/bar/
=3D> /bar/
irb(main):003:0> x=3D/#{a}#{b}/
=3D> /(?-mix:foo)(?-mix:bar)/

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

0
shortcutter (5831)
10/27/2009 1:04:08 PM
Reply:

Similar Artilces:

How make regex that means "contains regex#1 but NOT regex#2" ??
I'm looking over the docs for the re module and can't find how to "NOT" an entire regex. For example..... How make regex that means "contains regex#1 but NOT regex#2" ? Chris On 2008-07-01, seberino@spawar.navy.mil <seberino@spawar.navy.mil> wrote: > I'm looking over the docs for the re module and can't find how to > "NOT" an entire regex. (?! R) > How make regex that means "contains regex#1 but NOT regex#2" ? (\1|(?!\2)) should do what you want. Albert On Jul 1, 2:34=A0am, "A.T.Hofkamp" <h...@se-162....

perl regex to java regex
I am trying to convert a piece of code from using perl regexes to using java regexes. I need some help with converting the following two PERL syntax regex's to java syntax regex's. Can anyone please help 1. FIRST_PERL_REGEX = "/^[\\d]*$/" 2. SECOND_PERL_REGEX = "/^[\\d]*/" (All I am trying to do in the above regex's is try to look for an integer) essentially I need the equivalent FIRST_JAVA_REGEX and SECOND_JAVA_REGEX for the above. "Rick Venter" <rick_venter@yahoo.com> wrote in message news:e6f6eb95.0310290901.38be6c02@posting.google.com.....

regex =~ string or string =~ regex?
Sometime I saw you wrote regex =~ string, while sometime you wrote string =~ regex. What's their difference and what's the recommended way? Thanks. Jenn. On 01/04/2010 10:27 AM, Ruby Newbee wrote: > Sometime I saw you wrote regex =~ string, while sometime you wrote > string =~ regex. > What's their difference and what's the recommended way? Thanks. The first version invokes method Regexp#=~ and the second version invokes String#=~ - which happen to do roughly the same although I believe the second one to be a tad slower. I personally prefer the first form becau...

Ruby Regex
Hello, I am working on extracting some data from a file. Sample data in the file are as follows: Case 1: Direction abc,(xyz) Case 2: Direction fet I want to extract the content after Direction but before the comma(if present). The issue I am facing is that the comma is not always present. Otherwise a reg exp like: reg_exp = %r{Direction(.+),} would have given me the content. This would not work in the 2nd case. How to account for both the conditions. Any help would be appreciated... -- Posted via http://www.ruby-forum.com/. On 09.04.2009 16:44, Sriram Varahan wrote: > Hello...

regex
i would like to seek out "def" from this below expression: my $str2; my $str = "<abc>def</abc>"; if ($str =~ /abc/) { $str2 = # want to put "def" in $str2; } how can i write it properly? Thanks for any reply. On 04/09/15 07:42, Wandy Tang wrote: > i would like to seek out "def" from this below expression: > > my $str2; > my $str = "<abc>def</abc>"; IF you simply want to pick out what is between the character strings <abc> and </abc> then this will do it: my $str2; my ...

regex
I want to match mailto:null@void.com......gif in a string that contains many similar patterns. I used "mailto:(.[^"]*)" to match each "mailto: ..." string. The parans then allowed me to get at the email address. But now I realize I need more info from each match. I need to extend the match to the first 'gif' (the characters 'gif' would be the end of the string instead of ' " ' as in my first regex.) but I am having some trouble doing so. I have tried a number of different combinations but without the match I need. I know what I need. ...

regex
Hi for everyone, i need to parse a text and i need to found a text like this: <objInformation name="<%=Property.name%> version="1"> I try to parse this with this pattern but don't work: <objInformation(\s+\w+\s*=\s*([(["|']?<\s*%\s*=\s*Property.\w+\s*%\s*>["|']?) |(["|']?\w+["|']?)]))*> Anybody what is wrong in this regular expresion? Thanks, Alejandro "a_narancio@hotmail.com (Alejandro Narancio)" wrote in comp.lang.java: > Hi for everyone, i need to parse a text and i need to fo...

RegEx
why my program not work? wxString x = "Hello"; wxString y = "world"; wxString n = "123"; wxRegEx *r = new wxRegEx("e[a-z]*o"); wxRegEx r2("/[a-z]*/"); wxPrintf(_("regex [%s]\n"),r->GetMatch(x).mb_str()); I would lige to get "ello" At 13:45 30.01.2008 +0100, Zorro wrote: >why my program not work? > > wxString x = "Hello"; > wxString y = "world"; > wxString n = "123"; > wxRegEx *r = new wxRegEx("e[a-z]*o"); > wxRegEx r2("/[a-...

RegEx
Is there is a good video lesson out there that focuses on RegEx for use specifically in GNU bash or C11. -- Marek Novotny https://github.com/marek-novotny In article <wOqdnaQT4rX53G7JnZ2dnUU7-U2dnZ2d@giganews.com>, Marek Novotny <marek.novotny@marspolar.com> wrote: > Is there is a good video lesson out there that focuses on RegEx for use > specifically in GNU bash or C11. I don't know if there's any video, but http://www.regular-expressions.info/ is a good place to start looking for tutorials on regexp. -- Barry Margolin, barmar@alum.mit.edu...

regex
I'm digging through the Pragmatic Programmers book and had some questions about regex support. Initially they only mention .sub and .gsub. If I wanted to write a regex block using additional flags, say something like this: s/perl/ruby/igsm What that be expressed as: "my favorite programming language is perl".gsub(/perl/ism, 'ruby') ??? What about the 'x' option? Tom Allison wrote: > I'm digging through the Pragmatic Programmers book and had some > questions about regex support. > > Initially they only mention .sub and .gsub. > > If...

regex
Hi, What's the right expression to find exponent and argument in: sin<sup>2</sup>(B/2 - C/2) ? I need to change it to: sin(B/2 - C/2)^2 I've tried with "(sin|cos|tan|cot|sec|csc)<sup>(\d)+</sup>\(*\)" but this string is not compiled with Wild2RegEx() Also, I tried with "(sin|cos|tan|cot|sec|csc)<sup>(\d)+</sup>(*)". This is compiled by with Wild2RegEx() but element in HB_RegExAll() loses the right ")" Thanks a lot in advance Cesar Lozada > > What's the right expression to find exponent and argument in:...

regex
hi all, was hoping someone could provide a regex for the following situation - im pulling my hair out thanks PP -------- Q) password needs to contain at least one number and have a minimum length of 6 Psybar Phreak wrote on 27 sep 2003 in comp.lang.java.javascript: > hi all, was hoping someone could provide a regex for the following > situation - im pulling my hair out Already answered elswhere. Please do not multipost !!! And leave your hair alone ;-} -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress) ...

regex
Hi, in this regex I have two different results but why?Pattern p = Pattern.compile("[a-z&&[def]]");Matcher m = p.matcher("adef");boolean b = m.find(); // trueboolean b2 = Pattern.matches("[a-z&&[def]]", "adef"); // falseIf I try to use another regex tool as RegexBuddy I also have falseBut the is the intersection a regex standard feature?Thanks On 20/06/2007 12:45, josh wrote:> Hi, in this regex I have two different results but why?> > Pattern p = Pattern.compile("[a-z&&[def]]");> Matcher m = p.matcher("a...

Regex
Hi! I am making a website with PHP (of course) and have to make this transformation: <l>name</l> to <a href="blabla.php?id=name">name</a> How do I do that? Will I have to use several regex or is it possible that I can use only one? -- Greetings/Martin Nielsen-L�nn Martin Nielsen-L�nn wrote: > I am making a website with PHP (of course) and have to make this > transformation: <l>name</l> to <a href="blabla.php?id=name">name</a> > How do I do that? preg_replace("/<l>(.*?)<\/l>/" "<...