regular expressions galore

ruby

regular expressions galore

Postby Dirk Meijer » Thu, 27 Oct 2005 00:54:58 GMT


regexps can be so confusing, i need help on this one..
- i want one regexp to match a possible colon at the start (but no more),
followed by multiple word characters, or the following: / ' " # $ @
- another regular expression should match the opposite, no colon at the
start (but possibly colon later), followed by characters other than the
characters shown above.
 so far, i've got this:
 /^:?[\w|\/|\'|\"|#|$|@]+/
and
/^[^:]([^\w|\/|\'|\"|#|$|@]|:)+/
and it's not working :-(
can anyone enlighten my troubled mind?
greetings, Dirk.


Re: regular expressions galore

Postby Robert Klemme » Thu, 27 Oct 2005 01:02:56 GMT



Hm, you said "multiple word chars *or* one of your special chars".  To me 
that sounds as if you want

%r{^(?:\w+|[/'"#$@])}


You don't want the pipe symbol ("|") inside square brackets because here it 
doesn't mean "or" but is taken literally.  Same holds true for your other RX 
btw.


If you just want to match all strings that don't match the other RX then you 
could simply use !~ instead of =~:

irb(main):020:0> /foo/ =~ "foo"
=> 0
irb(main):021:0> /foo/ !~ "foo"
=> false

Kind regards

    robert


Re: regular expressions galore

Postby Kevin Ballard » Thu, 27 Oct 2005 01:05:53 GMT

Yeah, remove the vertical bars in your character class. Those are for
alternations in a group, they don't belong in a character class. Also,
you can remove the escaping of ' and " in your character class.


Re: regular expressions galore

Postby Jean-Claude Arbaut » Thu, 27 Oct 2005 01:11:10 GMT







I'd say:

(file test.rb)
if /^:?[\w\/\'\"#$@]*$/.match($_)
  puts $_
end

and to check:
ruby -n test.rb
:blablabla
..





Re: regular expressions galore

Postby Dirk Meijer » Thu, 27 Oct 2005 01:17:25 GMT


hi,


 no, sorry about that, i meant 'multiple characters which are word chars or
one of those'

 > and

 strange, that seemed to have the effect i wanted. (uhm.. nvm, figured that
out..)


 also doesn't seem to work properly.. but thanks for helping :-)


Re: regular expressions galore

Postby Jean-Claude Arbaut » Thu, 27 Oct 2005 01:24:46 GMT







Oupsss. I think this is better:

if %r{^:?[\w#"'/$@]*$}.match($_)
  puts $_
end


BTW, I get something strange:


With
    if /^:?[\w"'#$@]*$/.match($_); puts $_; end
the string :e#e does not match, but with
    if /^:?[\w#"'$@]*$/.match($_); puts $_; end
it does !
Any idea why ?






Re: regular expressions galore

Postby ts » Thu, 27 Oct 2005 01:35:19 GMT

>>>>> "J" == Jean-Claude Arbaut < XXXX@XXXXX.COM > writes:

J>     if /^:?[\w"'#$@]*$/.match($_); puts $_; end

 This is interpreted as

       /^:?[\w"'#{$@}]*$/

moulon% ruby -e 'raise "aa" rescue puts "#$@ -- #$!"'
-e:1 -- aa
moulon% 



Guy Decoux



Re: regular expressions galore

Postby Jean-Claude Arbaut » Thu, 27 Oct 2005 02:07:11 GMT






Wow! I won't forget that! Thanks.


Re: regular expressions galore

Postby Dirk Meijer » Thu, 27 Oct 2005 02:17:02 GMT


it's still not working for me :-(
!~ doesn't work anyway, because it only returns true or false, when i need
$& to be set.
i think this one is proper: /^:?[\w\/'"$@#]+/
but what would be the opposite of that?


Re: regular expressions galore

Postby ts » Thu, 27 Oct 2005 18:04:04 GMT

>>>>> "D" == Dirk Meijer < XXXX@XXXXX.COM > writes:

D> it's still not working for me :-(

 Give an example of what you are trying to match

D> !~ doesn't work anyway, because it only returns true or false, when i need
D> $& to be set.
D> i think this one is proper: /^:?[\w\/'"$@#]+/
D> but what would be the opposite of that?

 It's really depend on what you want to match. The opposite seems

   * it must not begin with one of the characters :\w/'"$@# (the empty
     string is in this case)

        OR

   * begin with one : not followed by one of the characters \w/'"$@#


Guy Decoux




Re: regular expressions galore

Postby William James » Thu, 27 Oct 2005 18:36:19 GMT




%r{ ^ : ? [ / ' " # $ @ ] + }x

and

%r{ ^ (?! : ) [^ / ' " # $ @ ] + }x


Re: regular expressions galore

Postby Dirk Meijer » Thu, 27 Oct 2005 23:11:57 GMT


got it to work now, in the middle of the night, suddenly realized i really
needed something else than what i was trying to get..
sorry for troubling you all, and thanks for the help :-)
greetings, Dirk.

 2005/10/26, William James < XXXX@XXXXX.COM >:





Return to ruby

 

Who is online

Users browsing this forum: No registered users and 73 guest