Need regex to match "^\n"

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

Hi:

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

or caret :slight_smile:

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.

···

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’ :slight_smile:

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

irb(main):009:0> “^foo\n” =~ /[1]/
=> 0
irb(main):010:0> “foo\n” =~ /[2]/
=> nil

irb(main):012:0> “^\n” =~ /[3]$/
=> 0
irb(main):013:0> “^ \n” =~ /[4]$/
=> nil

HTH :slight_smile:

···

On Mon, Jul 07, 2003 at 07:55:52AM +0900, Jim Freeze wrote:


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


  1. ^ ↩︎

  2. ^ ↩︎

  3. ^ ↩︎

  4. ^ ↩︎

You’ve gotten some other workable suggestions, but in case this one’s
interesting to you:

line =~ /^\x5e/

All in the spirit of TIMTOWTDI, of course.

– Dossy

···

On 2003.07.07, Jim Freeze jim@freeze.org wrote:

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

line = “^\n”


Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)

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> /[1]/ =~ line1.strip
=> 0
irb(main):011:0> /[2]/ =~ line2.strip
=> nil

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


Jim Freeze


  1. ^ ↩︎

  2. ^ ↩︎

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

line = “^\n”

You’ve gotten some other workable suggestions, but in case this one’s
interesting to you:

line =~ /^\x5e/

line =~ /[1]$/

···

On Sun, 2003-07-06 at 18:03, Dossy wrote:

On 2003.07.07, Jim Freeze jim@freeze.org wrote:

All in the spirit of TIMTOWTDI, of course.

– Dossy


  1. ^ ↩︎

% cat caret
puts (/[1]/ =~ “^\n”)

% ruby caret
caret:1: invalid regular expression; empty character class: /[2]/

···

On Monday, 7 July 2003 at 10:05:22 +0900, Aredridel wrote:

line =~ /[3]$/


Jim Freeze

Larkinson’s Law:
All laws are basically false.


  1. ^ ↩︎

  2. ^ ↩︎

  3. ^ ↩︎

Jim Freeze jim@freeze.org writes:

line =~ /[1]$/

% cat caret
puts (/[2]/ =~ “^\n”)

try this:

puts (“^\n” =~ /[3]/)

% ruby caret
caret:1: invalid regular expression; empty character class: /[4]/

Just remember, string goes on the left, regex goes on the right.

···

On Monday, 7 July 2003 at 10:05:22 +0900, Aredridel wrote:


Samuel


  1. ^ ↩︎

  2. ^ ↩︎

  3. ^ ↩︎

  4. ^ ↩︎

line =~ /[1]$/

% cat caret
puts (/[2]/ =~ “^\n”)

% ruby caret
caret:1: invalid regular expression; empty character class: /[3]/

Oh, right. I knew that. /^^/.

···

On Sun, 2003-07-06 at 19:44, Jim Freeze wrote:

On Monday, 7 July 2003 at 10:05:22 +0900, Aredridel wrote:


  1. ^ ↩︎

  2. ^ ↩︎

  3. ^ ↩︎

Which was in Jim’s original posting :slight_smile:

···

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: /[1]/

Oh, right. I knew that. /^^/.


  1. ^ ↩︎

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

···

On Monday, 7 July 2003 at 11:14:29 +0900, Samuel Tesla wrote:

Jim Freeze jim@freeze.org writes:

try this:

puts (“^\n” =~ /[1]/)

% ruby caret
caret:1: invalid regular expression; empty character class: /[2]/

Just remember, string goes on the left, regex goes on the right.


Jim Freeze

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


  1. ^ ↩︎

  2. ^ ↩︎

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

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.

I always wondered about that. It “reads” funny to my brain that way but I
wasn’t consulted. =D Too, it’s easy enough to do either direction, so no
worries.

I think regex-on-right is a perlism, but even in Ruby if you use =~ on two
strings, it’s the one on the right which is treated as a regex:

irb(main):003:0> “abc” =~ “b”
=> 1
irb(main):004:0> “b” =~ “abc”
=> nil

Cheers,

Brian.

···

On Tue, Jul 08, 2003 at 02:52:44AM +0900, Michael Campbell wrote:

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.

I always wondered about that. It “reads” funny to my brain that way but I
wasn’t consulted. =D Too, it’s easy enough to do either direction, so no
worries.

Maybe it has to do with Matz being used to reading RTL and not LTR like
us 'merkins.

– Dossy

···

On 2003.07.08, Michael Campbell michael_s_campbell@yahoo.com wrote:

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.

I always wondered about that. It “reads” funny to my brain that way but I
wasn’t consulted. =D Too, it’s easy enough to do either direction, so no
worries.


Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)

Brian Candler B.Candler@pobox.com writes:

I think regex-on-right is a perlism, but even in Ruby if you use =~ on two
strings, it’s the one on the right which is treated as a regex:

That’s certainly where I picked it up.