Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [nuby] it seems that (?!...) doesn't work for me

Reply
Thread Tools

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

 
 
Lionel Thiry
Guest
Posts: n/a
 
      10-13-2004
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


 
Reply With Quote
 
 
 
 
ts
Guest
Posts: n/a
 
      10-13-2004
>>>>> "L" == Lionel Thiry <> 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


 
Reply With Quote
 
 
 
 
Lionel Thiry
Guest
Posts: n/a
 
      10-13-2004
ts a écrit :

>>>>>>"L" == Lionel Thiry <> 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


 
Reply With Quote
 
ts
Guest
Posts: n/a
 
      10-13-2004
>>>>> "L" == Lionel Thiry <> 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





 
Reply With Quote
 
Florian G. Pflug
Guest
Posts: n/a
 
      10-13-2004
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




 
Reply With Quote
 
Lionel Thiry
Guest
Posts: n/a
 
      10-14-2004
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
it seems to work for Chriz... anthonyberet Computer Support 13 08-26-2004 08:58 AM
Responise.Write() seems not to work in codebind. Wootaek Choi ASP .Net 1 02-10-2004 04:45 AM
synchronize seems not work lonelyplanet999 Java 6 11-16-2003 02:01 PM
Expiration date seems not to work Frederic Gignac ASP .Net 2 07-08-2003 01:27 PM



Advertisments