# Simple regexp: Matching CRLF in a Windows file

**URL:** <https://www.ruby-forum.com/t/simple-regexp-matching-crlf-in-a-windows-file/60810>\
**Category:** Ruby\
**Created:** [May 17, 2006, 6:33pm UTC](https://www.ruby-forum.com/t/simple-regexp-matching-crlf-in-a-windows-file/60810 "2006-05-17T18:33:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [May 17, 2006, 6:33pm UTC](https://www.ruby-forum.com/t/simple-regexp-matching-crlf-in-a-windows-file/60810/1 "2006-05-17T18:33:44Z")

</div>

All,

I’m trying to do subs on CRLF in a Windows file, but I’m having a time  
of it trying to get the regexp working.

I am sure that I have a text file with Windows newlines (CRLF) embedded  
in it

This doesn’t work:

newline = “\C-J\C-M”  
puts IO.read(@document.path) =~ /#{newline}/

This doesn’t work:

newline = “\n\r”  
puts IO.read(@document.path) =~ /#{newline}/

Do I need to escape the control characters somehow so that I can match  
on them?

I’m assuming it’s possible to use control characters in a regexp.

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [May 17, 2006, 6:43pm UTC](https://www.ruby-forum.com/t/simple-regexp-matching-crlf-in-a-windows-file/60810/2 "2006-05-17T18:43:29Z")

</div>

Apparently just matching on \n works.

So, Ruby is really smart enough to use \n as a platform-independent  
newline character? Impressive.

Wes

Wes G. wrote:

> All,
> 
> I’m trying to do subs on CRLF in a Windows file, but I’m having a time  
> of it trying to get the regexp working.
> 
> I am sure that I have a text file with Windows newlines (CRLF) embedded  
> in it
> 
> This doesn’t work:
> 
> newline = “\C-J\C-M”  
> puts IO.read(@document.path) =~ /#{newline}/
> 
> This doesn’t work:
> 
> newline = “\n\r”  
> puts IO.read(@document.path) =~ /#{newline}/
> 
> Do I need to escape the control characters somehow so that I can match  
> on them?
> 
> I’m assuming it’s possible to use control characters in a regexp.
> 
> Thanks,  
> Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [May 17, 2006, 7:22pm UTC](https://www.ruby-forum.com/t/simple-regexp-matching-crlf-in-a-windows-file/60810/3 "2006-05-17T19:22:19Z")

</div>

Wes G. wrote:

> > I am sure that I have a text file with Windows newlines (CRLF) embedded  
> > in it
> > 
> > This doesn’t work:
> > 
> > newline = “\C-J\C-M”  
> > puts IO.read(@document.path) =~ /#{newline}/

And then:

> Apparently just matching on \n works.
> 
> So, Ruby is really smart enough to use \n as a platform-independent  
> newline character? Impressive.

Ruby on Windows automatically replaces \r\n with \n on input and  
vice-versa on output unless you tell open to use binary mode:

irb\> File.open(“test.txt”,“w”) {|f| f.print “\n” } # write, normal mode  
=\> nil  
irb\> File.open(“test.txt”,“r”) {|f| f.read } # read, normal mode  
=\> “\n”  
irb\> File.open(“test.txt”,“rb”) {|f| f.read } # read, binary mode  
=\> “\r\n”  
irb\> File.open(“test.txt”,“wb”) {|f| f.print “\n” } # write, binary mode  
=\> nil  
irb\> File.open(“test.txt”,“rb”) {|f| f.read }  
=\> “\r\n”

Cheers,  
Dave
