Solved

trying to get text between certain words

Posted on 2013-02-06
6
218 Views
Last Modified: 2013-02-08
The text below looks messy but thats how we get files sent to us sometimes.
I want to retrieve all text between "JOIN" and "="

sample text....

SELECT
*
  FROM mytable ppst
       LEFT OUTER JOIN mytable2 pps
                ON ppst.id = pps.id
       LEFT OUTER JOIN mytable3 ps                ON ps.id = pps.p_id
       LEFT OUTER JOIN test p                                  ON p.id = pps.prod_id                        

                                          -- make join inner when data in product
       LEFT OUTER JOIN product p2                            ON p2.id = ppst.to_product_id                                

                              -- make join inner when data in product
       LEFT OUTER JOIN filer f                                  

ON f.id = ppst.did
-- WHERE ps.published_by IS NOT NULL


so - if the regular expression works the it should return
##############################################
mytable2 pps
                ON ppst.id
mytable3 ps                ON ps.id
test p                                  ON p.id
product p2                            ON p2.id
filer f                                  

ON f.id
##############################################
ideally I would like ..
mytable2 pps ON ppst.id
mytable3 ps ON ps.id
test p ON p.id
product p2 ON p2.id
filer f ON f.id

I'm trying this expression but its not working

if record =~/join(.|\n\r\f)*=/i
            puts record
0
Comment
Question by:PHIL Sawyer
  • 4
  • 2
6 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 38861370
Match the text you want with this pattern (using ignore case and singleline modifiers):
(?<=join).*?(?==)
then to remove the surplus spaces substitute pattern
\s{2,}
with a single space.

If you actually have the comments (starting with --) in the text, then you may need to remove those prior to the above.

I'm not a Ruby programmer, by the way; my specialty is regular expressions. Note also there is a Regular Expressions topic with other experts who contribute...
0
 

Author Comment

by:PHIL Sawyer
ID: 38863144
if record =~/(?<=join).*?(?==)/i then

the above misses the 2 lines
 LEFT OUTER JOIN mytable2 pps
                ON ppst.id = pps.id

and it returns the whole line and not the text between join and =

Regards
0
 

Author Comment

by:PHIL Sawyer
ID: 38863468
just to correct my above posting ...
it does capture text between "join and =" and only returns data between those variables but does not return the first 2 lines - must be to do with = is on the next line

Regards
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 38865622
Yes, you'll need the singleline modifier. Like this, I believe:

 if record =~/(?<=join).*?(?==)/is then
0
 

Author Comment

by:PHIL Sawyer
ID: 38867959
Thanks
0
 

Author Closing Comment

by:PHIL Sawyer
ID: 38867960
Thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question