0

I have the following string:

" 105 NE 12 Avenue, Apt #9,\nHallandale Beach, FL 33309"

It should match the following regex:

/\s+(\d+\s+.+,.+,.+\d+)/

Let's break it down:

\s+ #=> 

\d+ #=> 105

\s+ #=>

.+  #=> NE 12 Avenue

,   #=> ,

.+  #=> Apt #9

,   #=> ,

.+  #=> \nHallandale Beach, FL 

\d+ #=> 33309

However, it does not match:

> " 105 NE 12 Avenue, Apt #9,\nHallandale Beach, FL 33309" =~ /\s+(\d+\s+.+,.+,.+\d+)/
 => nil 
> $1
 => nil

What am I missing here?

Donato
  • 5,990
  • 7
  • 41
  • 75
  • You have a newline character? Is that a real newline or literal string `\n`? Whatever you mean it is interpreted as a newline character in provided line of code and `.` doesn't match it. – revo Apr 26 '18 at 20:03
  • . does not match \n newline character? Are you sure? I thought . matches any character. How am I supposed to match newline character then? – Donato Apr 26 '18 at 20:06
  • 2
    It doesn't match a newline character unless you enable `m` flag in ruby. – revo Apr 26 '18 at 20:08
  • 3
    `/\s+(\d+\s+.+,.+,.+\d+)/m` – Wiktor Stribiżew Apr 26 '18 at 20:08
  • But if you want to match exactly a newline at this position then i would not go for _m_ flag. Just match the newline. ```\s+(\d+\s+.+,\n.+,.+\d+)``` See http://rubular.com/r/16s4X1ardX – Pascal Apr 27 '18 at 06:15

0 Answers0