Solved

trying to get text between certain words

Posted on 2013-02-06
6
212 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now