f



Ruby Regexp why this expression does not match?

I want to use this regex to match any block comment (c-style) in a string. 
But why the below does not?


rblockcmt = Regexp.new "/\\*[.\s]*?\\*/"  # match block comment
p rblockcmt=~"/* 22/Nov - add fee update */"

==> nil
0
vunangluc
12/3/2016 4:51:46 AM
comp.lang.ruby 48886 articles. 0 followers. Post Follow

3 Replies
546 Views

Similar Articles

[PageSpeed] 31

On 03.12.2016 05:51, vunangluc@gmail.com wrote:
> I want to use this regex to match any block comment (c-style) in a string.
> But why the below does not?
>
>
> rblockcmt = Regexp.new "/\\*[.\s]*?\\*/"  # match block comment
> p rblockcmt=~"/* 22/Nov - add fee update */"
>
> ==> nil

Because a dot is not special in a character class.

Btw. why do you use Regexp.new and not write the expression directly?

Cheers

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
0
Robert
12/3/2016 8:13:38 AM
Because I dont know how, to escape / in the // form.

Note. In dotnet that regex work for me.

What is shortest regex for my purpose?
0
vunangluc
12/3/2016 8:28:26 AM
On 03.12.2016 09:28, vunangluc@gmail.com wrote:
> Because I dont know how, to escape / in the // form.

Use the backslash, Luke.

> Note. In dotnet that regex work for me.

Note, Ruby != .Net.

> What is shortest regex for my purpose?

%r{/\*.*?\*/}m
/\/\*.*?\*\//m

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
0
Robert
12/3/2016 10:27:29 AM
Reply: