From: Josh Cheek Date: 2013-05-24T13:38:25+09:00 Subject: Re: Regular expression to find a break in a pattern --089e013c67067281f204dd6f5e03 Content-Type: text/plain; charset=ISO-8859-1 On Thu, May 23, 2013 at 11:13 AM, Joel Pearson wrote: > I have a large file which lots of gibberish in and I'm trying to find > the meaningful sections. > > Essentially I'll have something like this: > > ________________ > > To: "1313131" > From: "1313131" > random data lines > > To: "1313132" > From: "1313132" > random data lines > > To: "1313133" > From: "1313132" > random data lines > > To: "1313134" > From: "1313134" > random data lines > > ________________ > > What I need to do is locate the line(s) where From is different from To. > In this case, the one From "1313132" To "1313133". > > I don't know how to do this kind of match, but I assume that Ruby has a > way? > > -- > Posted via http://www.ruby-forum.com/. > > Here is a regex that works for your example data. text = ' To: "1313131" From: "1313131" random data lines To: "1313132" From: "1313132" random data lines To: "1313133" From: "1313132" random data lines To: "1313134" From: "1313134" random data lines To: "abc" From: "def" random data lines ' regex = /To: "(.*?)"\nFrom: "(?!\1)(.*?)"$/ text.scan(regex) # => [["1313133", "1313132"], ["abc", "def"]] --089e013c67067281f204dd6f5e03 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Thu, May 23, 2013 at 11:13 AM, Joel Pearson <lists@ruby-forum.com> wrote:


Here is= a regex that works for your example data.

tex= t =3D '
To: "1313131"
From: "1313131= "
random data lines

To: "1313132"
From: "1313132"
random data lines

=
To: "1313133"
From: "1313132"
random data lines

To: "1313134"
From: "1313134"
random data lines

=
To: "abc"
From: "def"
random data lines
'

regex =3D /To: &= quot;(.*?)"\nFrom: "(?!\1)(.*?)"$/

= text.scan(regex) =A0# =3D> [["1313133", "1313132"], = ["abc", "def"]]
--089e013c67067281f204dd6f5e03--