Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

trying to get text between certain words

Posted on 2013-02-06
6
Medium Priority
?
233 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 35

Accepted Solution

by:
Terry Woods earned 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month8 days, 7 hours left to enroll

876 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