Regular expressions galore

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 :frowning:
can anyone enlighten my troubled mind?
greetings, Dirk.

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|\/|\'|\"|#|$|@]+/

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

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

and
/^[^:]([^\w|\/|\'|\"|#|$|@]|:)+/
and it's not working :frowning:

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.

can anyone enlighten my troubled mind?
greetings, Dirk.

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

路路路

Dirk Meijer <hawkman.gelooft@gmail.com> wrote:

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.

Dirk Meijer wrote:

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 :frowning:
can anyone enlighten my troubled mind?
greetings, Dirk.

I'd say:

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

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

hi,

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

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

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

> and

> /^[^:]([^\w|\/|\'|\"|#|$|@]|:)+/
> and it's not working :frowning:

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.

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

can anyone enlighten my troubled mind?
> greetings, Dirk.

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

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

Jean-Claude Arbaut wrote:

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 ?

路路路

I'd say:

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

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

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

This is interpreted as

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

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

Guy Decoux

ts wrote:

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

This is interpreted as

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

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

Guy Decoux

Wow! I won't forget that! Thanks.

it's still not working for me :frowning:
!~ 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?

it's still not working for me :frowning:

Give an example of what you are trying to match

!~ 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?

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

Dirk Meijer wrote:

it's still not working for me :frowning:
!~ 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?

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

and

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

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 :slight_smile:
greetings, Dirk.

路路路

2005/10/26, William James <w_a_x_man@yahoo.com>:

Dirk Meijer wrote:
> it's still not working for me :frowning:
> !~ 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?

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

and

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