Usage of Regexp::EXTENDED

How does it work ?

I tried
assert_match(/bc/x, “ab cd”)
But it fails…

ProgrammingRuby says:
EXTENDED Ignore spaces and newlines in regexp.

···


Simon Strandgaard

BTW: I am exercising Rubys regex engine, here:
http://rubyforge.org/cgi-bin/cvsweb.cgi/projects/experimental/nfa_to_dfa/tes
t_ruby_regex.rb?rev=1.7&content-type=text/x-cvsweb-markup&cvsroot=aeditor

Hi,

I tried
assert_match(/bc/x, “ab cd”)
But it fails…

/bc/x has no spaces.

ProgrammingRuby says:
EXTENDED Ignore spaces and newlines in regexp.

But not ignore spaces and newlines in the target string.

···

At Wed, 8 Oct 2003 20:48:17 +0900, Simon Strandgaard wrote:


Nobu Nakada

Try: assert_match(/b c/x, “abcd”)

···

On Wed, 2003-10-08 at 07:48, Simon Strandgaard wrote:

How does it work ?

I tried
assert_match(/bc/x, “ab cd”)
But it fails…


– Jim Weirich jweirich@one.net http://onestepback.org

“Beware of bugs in the above code; I have only proved it correct,
not tried it.” – Donald Knuth (in a memo to Peter van Emde Boas)

that works… but

assert_match(/b \nc/x, “abcd”)

fails ?

programming-ruby says that newlines will be ignored?

···

On Wed, 08 Oct 2003 21:58:42 +0900, Jim Weirich wrote:

On Wed, 2003-10-08 at 07:48, Simon Strandgaard wrote:

How does it work ?

I tried
assert_match(/bc/x, “ab cd”)
But it fails…

Try: assert_match(/b c/x, “abcd”)


Simon Strandgaard

Physical newlines. The \n is part of the pattern

assert_match(/b
                      c/x, "abcd")
···

On Wednesday, Oct 8, 2003, at 07:49 US/Central, Simon Strandgaard wrote:

assert_match(/b \nc/x, “abcd”)

fails ?

programming-ruby says that newlines will be ignored?

Hi –

How does it work ?

I tried
assert_match(/bc/x, “ab cd”)
But it fails…

Try: assert_match(/b c/x, “abcd”)

that works… but

assert_match(/b \nc/x, “abcd”)

fails ?

programming-ruby says that newlines will be ignored?

I think it’s only literal newlines – so that if you want to actually
match a newline, there’s still a way to do it. (And since the idea
behind /x is to increase whitespace and readability, there’s not much
advantage skipping over “\n” :slight_smile: Here’s an example with a literal
newline:

irb(main):003:0> /a
irb(main):004:0/ b/x.match(“ab”)
=> #MatchData:0x400d2ba8

David

···

On Wed, 8 Oct 2003, Simon Strandgaard wrote:

On Wed, 08 Oct 2003 21:58:42 +0900, Jim Weirich wrote:

On Wed, 2003-10-08 at 07:48, Simon Strandgaard wrote:


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

I was blind, and now I see. Thanks comp.lang.ruby :wink:

···

On Wed, 08 Oct 2003 23:02:27 +0900, Dave Thomas wrote:

On Wednesday, Oct 8, 2003, at 07:49 US/Central, Simon Strandgaard wrote:

assert_match(/b \nc/x, “abcd”)

fails ?

programming-ruby says that newlines will be ignored?

Physical newlines. The \n is part of the pattern

assert_match(/b
                      c/x, "abcd")


Simon Strandgaard

Feeding the same string into // and into Regexp.new,
yields to different results. A bit inconsist…

def test_option_extended3
	# ignoring newline
	re = Regexp.new("b\nc", Regexp::EXTENDED)
	assert_match(re, "abcd")
end
def test_option_extended4
	# does not ignore newline... makes no sense?
	assert_no_match(/b\nc/, "abcd")
end 

Just my thought :wink:

···


Simon Strandgaard

Feeding the same string into // and into Regexp.new,
yields to different results. A bit inconsist…

inconsistent<<

def test_option_extended3
# ignoring newline
re = Regexp.new(“b\nc”, Regexp::EXTENDED)
assert_match(re, “abcd”)
end
def test_option_extended4
# does not ignore newline… makes no sense?

	assert_no_match(/b\nc/x, "abcd")

I forgot the ‘x’ ^^^

end

Just my thought :wink:

revision 0.2: spell correction.

···

On Wed, 08 Oct 2003 16:51:52 +0200, Simon Strandgaard wrote:


Simon Strandgaard

Simon Strandgaard wrote:

Feeding the same string into // and into Regexp.new,
yields to different results. A bit inconsist…

def test_option_extended3
# ignoring newline
re = Regexp.new(“b\nc”, Regexp::EXTENDED)
assert_match(re, “abcd”)
end

In this case the \n will have already been replaced by a literal
newline. So Regexp.new receives

“a
b”

as it’s first parameter.

def test_option_extended4
# does not ignore newline… makes no sense?
assert_no_match(/b\nc/, “abcd”)
end

In this case the \n won’t have been interpolated before constructing the
regexp.

Best Regards

Mark Sparshatt