[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[nuby] it seems that (?!...) doesn't work for me

Lionel Thiry

10/13/2004 1:06:00 PM

I'd like to make some kind of substitution like in a shell: "${var}".
For that purpose, I use this pattern: /\$\{(.*?)\}/.

But I also want to be able to escape those special caracters $, { and }.
Examples:
"${var\\}weird}" # which is a variable with "var{weird" as name
"\\${var}" # which is the simple string "${var}" with no substitution

Then I wrote that pattern:
/(?!\\)\$\{(.*?)(?!\\)\}/

But, well, it doesn't work at all.

After some tests:
puts "ok" if "\\{".match(/\\\{/) # display "ok"
puts "not ok" if "\\{".match(/(?!\\)\{/) # display "not ok"

It seems that (?!\\) doesn't fullfill its role of "match if re in (?!re)
doesn't match". What's wrong with my code? Please help!

My ruby version is the "One-Click Installer - Windows" v1.8.1-13.

Lio


6 Answers

ts

10/13/2004 2:30:00 PM

0

>>>>> "L" == Lionel Thiry <lthiry@skynet.be> writes:

L> It seems that (?!\\) doesn't fullfill its role of "match if re in (?!re)
L> doesn't match". What's wrong with my code?

(?!re) is a zero-width negative look-*ahead* assertion. Apparently you
want a zero-width negative look-*behind* assertion which is (?<!re) and
implemented only in Oniguruma


svg% cat b.rb
#!./ruby
["${var\\}weird}", "\\${var}", "${var}"].each do |m|
p "#{m} -- #$1" if m.match(/(?<!\\)\$\{(.*?)(?<!\\)\}/)
end
svg%

svg% ruby b.rb
"${var\\}weird} -- var\\}weird"
"${var} -- var"
svg%



Guy Decoux


Lionel Thiry

10/13/2004 3:07:00 PM

0

ts a écrit :

>>>>>>"L" == Lionel Thiry <lthiry@skynet.be> writes:
>
>
> L> It seems that (?!\\) doesn't fullfill its role of "match if re in (?!re)
> L> doesn't match". What's wrong with my code?
>
> (?!re) is a zero-width negative look-*ahead* assertion. Apparently you
> want a zero-width negative look-*behind* assertion which is (?<!re) and
> implemented only in Oniguruma
That's right, that's probably what I want... but how do I use oniguruma?



Lionel Thiry


ts

10/13/2004 3:20:00 PM

0

>>>>> "L" == Lionel Thiry <lthiry@skynet.be> writes:

L> That's right, that's probably what I want... but how do I use oniguruma?

Oniguruma is in 1.9, but be carefull and don't forget that the regexp
engine is stupid

svg% ruby -e 'puts "not" unless "\\\\${var}".match(/(?<!\\)\$\{(.*?)(?<!\\)\}/)'
not
svg%



Guy Decoux





Florian Pflug

10/13/2004 8:25:00 PM

0

Lionel Thiry wrote:

> I'd like to make some kind of substitution like in a shell: "${var}".
> For that purpose, I use this pattern: /\$\{(.*?)\}/.
>
> But I also want to be able to escape those special caracters $, { and
> }. Examples:
> "${var\\}weird}" # which is a variable with "var{weird" as name
> "\\${var}" # which is the simple string "${var}" with no substitution

Use: /(^|[^\\]|(\\\\)+)\$\{(([^\}\\]|(\\.))*)\}/

I testes this with:
puts
(STDIN.readlines.join("\n").gsub(/(^|[^\\]|(\\\\)+)\$\{(([^\}\\]|(\\.))*)\}/m)
{|m| "#{$1}--#{$3.gsub(/\\(.)/, "\\1")}--"})
And it seems to work (replaces ${var} with --var--, but only if not
escaped).

The first part (/(^|[^\\]|(\\\\)+)/) basically means "either
start-of-string, non-backslash, or an even amount of backslash".
The part (/\{(([^\}\\]|(\\.))*)\}/) means "start with {, and than allow
any number of groups that consist of either a sequence of non-} and
non-\, or
of an escaped character (/\\./).

greetings, Florian Pflug




Lionel Thiry

10/14/2004 10:14:00 PM

0

Florian G. Pflug a écrit :

> Lionel Thiry wrote:
>
>> I'd like to make some kind of substitution like in a shell: "${var}".
>> For that purpose, I use this pattern: /\$\{(.*?)\}/.
>>
>> But I also want to be able to escape those special caracters $, { and
>> }. Examples:
>> "${var\\}weird}" # which is a variable with "var{weird" as name
>> "\\${var}" # which is the simple string "${var}" with no substitution
>
>
> Use: /(^|[^\\]|(\\\\)+)\$\{(([^\}\\]|(\\.))*)\}/
>
> I testes this with:
> puts
> (STDIN.readlines.join("\n").gsub(/(^|[^\\]|(\\\\)+)\$\{(([^\}\\]|(\\.))*)\}/m)
> {|m| "#{$1}--#{$3.gsub(/\\(.)/, "\\1")}--"})
> And it seems to work (replaces ${var} with --var--, but only if not
> escaped).
>
> The first part (/(^|[^\\]|(\\\\)+)/) basically means "either
> start-of-string, non-backslash, or an even amount of backslash".
> The part (/\{(([^\}\\]|(\\.))*)\}/) means "start with {, and than allow
> any number of groups that consist of either a sequence of non-} and
> non-\, or
> of an escaped character (/\\./).
>
> greetings, Florian Pflug

Thanks a lot for the help, but the pattern you gave is not perfect:

re = /(^|[^\\]|(\\\\)+)\$\{(([^\}\\]|(\\.))*)\}/
'pre${var}'.match # => prematch is "pr"

Lionel Thiry


yansimon52

1/8/2009 3:03:00 PM

0

On Jan 8, 8:30 pm, publeak <publ...@gmail.com> wrote:
> Muslims consistently use the phrase "' God Willing" to commit many
> evil acts.
>
> If we are to believe these muslims Allah should be blood thirsty god.

When these illiterates put their 101% trust in believing that Allah is
the Supreme God........it comes with a full course dinner
set.........Firstly, they were promise with 'Frills, Thrills and
Misery'.............instead of the dreamland's 72 virgins..........Not
only that.......their Allah eventually help to alter their face's
features...........on those too pious type older muslim......changing
their eye's look into some kind of 'devious look' that need to put on
dark glasses just to hide from public view..........Namely ppl like
the Taliban chief 'Mullah Oli Mohamad'..when you look at his
eye.......an almost squinted eye look ...............Malaysia
Opposition (PAS) chief............oso known to many malaysian as a
spiritual healer........been doing alot of prayer session during
nightfall had already turned his eye-look into some kind of deep
'charming look'........
Problem is..........these ignorant ppl still not aware that citing
those verses during nightfall cast bad spell to themselves such as,
altering their face's features into some kind of undesirable eye-
look.........

If their so-called God Allah is sooooooo good or the pure type coming
from the Brightside..........why must these decent and pious ppl got
to face this sort of undesireable thing happening to them