[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Need regex to match "^\n"

Jim Freeze

7/6/2003 10:56:00 PM

Hi:

I am looking for a regex that will match a line with a single
carrot:

line = "^\n"

However, the obvious does not seem to work:


irb(main):001:0> line1 = "^\n"
=> "^\n"
irb(main):002:0> line2 = "fred"
=> "fred"
irb(main):003:0> /^^/ =~ line1
=> 0
irb(main):004:0> /^^/ =~ line2
=> 0

Using /^\^/ gives an error.

What I have done to temporarily solve the problem is to use:

irb(main):012:0> /^[ ^]/ =~ line1.strip
=> 0
irb(main):011:0> /^[ ^]/ =~ line2.strip
=> nil

Does anyone know how to match '^' at the beginning of a line?

--
Jim Freeze
----------

6 Answers

Brian Candler

7/6/2003 11:02:00 PM

0

On Mon, Jul 07, 2003 at 07:55:52AM +0900, Jim Freeze wrote:
> Hi:
>
> I am looking for a regex that will match a line with a single
> carrot:

or caret :-)

> line = "^\n"
>
> However, the obvious does not seem to work:
>
>
> irb(main):001:0> line1 = "^\n"
> => "^\n"
> irb(main):002:0> line2 = "fred"
> => "fred"
> irb(main):003:0> /^^/ =~ line1
> => 0
> irb(main):004:0> /^^/ =~ line2
> => 0
>
> Using /^\^/ gives an error.

This looks like an irb-ism. I get the same as you within irb, but within
real code:

$ cat x.rb
line1 = "^\n"
line2 = "fred"

p line1 =~ /^\^/
p line2 =~ /^\^/

$ ruby x.rb
0
nil
$ ruby -v
ruby 1.6.8 (2002-12-24) [i386-freebsd4.8]

Regards,

Brian.

jjenning

7/6/2003 11:05:00 PM

0

On Mon, Jul 07, 2003 at 07:55:52AM +0900, Jim Freeze wrote:
> I am looking for a regex that will match a line with a single
> carrot:

(fwiw, that''s ''caret'' :)

> Does anyone know how to match ''^'' at the beginning of a line?

irb(main):009:0> "^foo\n" =~ /^[\^]/
=> 0
irb(main):010:0> "foo\n" =~ /^[\^]/
=> nil

irb(main):012:0> "^\n" =~ /^[\^]$/
=> 0
irb(main):013:0> "^ \n" =~ /^[\^]$/
=> nil

HTH :)

--
01CB B175 70D8 2E39 CA13 AEA6 3A2B 2219 31CD 5381

Brian Candler

7/7/2003 7:47:00 AM

0

On Mon, Jul 07, 2003 at 12:06:12PM +0900, Aredridel wrote:
> On Sun, 2003-07-06 at 19:44, Jim Freeze wrote:
> > % ruby caret
> > caret:1: invalid regular expression; empty character class: /^[^]/
>
> Oh, right. I knew that. /^\^/.

Which was in Jim''s original posting :-)

Robert Klemme

7/7/2003 8:16:00 AM

0


The obvious solution works for me:

irb(main):011:0> line = "^\n"
"^\n"
irb(main):012:0> line =~ /^\^$/ and puts "matches!"
matches!
nil
irb(main):013:0>

robert


"Jim Freeze" <jim@freeze.org> schrieb im Newsbeitrag
news:20030706190559.A19470@freeze.org...
> Hi:
>
> I am looking for a regex that will match a line with a single
> carrot:
>
> line = "^\n"
>
> However, the obvious does not seem to work:
>
>
> irb(main):001:0> line1 = "^\n"
> => "^\n"
> irb(main):002:0> line2 = "fred"
> => "fred"
> irb(main):003:0> /^^/ =~ line1
> => 0
> irb(main):004:0> /^^/ =~ line2
> => 0
>
> Using /^\^/ gives an error.
>
> What I have done to temporarily solve the problem is to use:
>
> irb(main):012:0> /^[ ^]/ =~ line1.strip
> => 0
> irb(main):011:0> /^[ ^]/ =~ line2.strip
> => nil
>
> Does anyone know how to match ''^'' at the beginning of a line?
>
> --
> Jim Freeze
> ----------
>

Jim Freeze

7/7/2003 11:43:00 AM

0

On Monday, 7 July 2003 at 11:14:29 +0900, Samuel Tesla wrote:
> Jim Freeze <jim@freeze.org> writes:
>
> try this:
>
> puts ("^\n" =~ /^[^]/)
>
> > % ruby caret
> > caret:1: invalid regular expression; empty character class: /^[^]/
>
> Just remember, string goes on the left, regex goes on the right.
>

Actually, it doesn''t matter, but Matz prefers the regex on the left.


--
Jim Freeze
----------
Be a better psychiatrist and the world will beat a psychopath to your
door.

Samuel Tesla

7/7/2003 5:20:00 PM

0

Jim Freeze <jim@freeze.org> writes:
> > Just remember, string goes on the left, regex goes on the right.
> >
>
> Actually, it doesn''t matter, but Matz prefers the regex on the left.

Heh. Learn something new every day.

Thanks,
Samuel