Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 19th, 2013, 05:53 AM
smb1970 smb1970 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 1 smb1970 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 34 m 15 sec
Reputation Power: 0
Help with a regex that reads the data between two strings with irregular no. of \n

Hi,
I'm fairly new to regex and I'm struggling to get a string that can scrape data from a web page.

I need all the data between two fixed points - lets define them as "string". However the data in between could span across anything from 1 up to 6 lines. So for example it might look like this ( I've bolded string for visibility, it would not be bolded in the file ).

string
12312\n
asd\n
string fsdsg\n
asgsdgfd sdfsd\n
<saef 12n\
af> sf \n
123\n
string\n
123\n
string gasfsfdsg\n

In this case I would be looking to select

12312\n
asd\n

fsdsg\n
asgsdgfd sdfsd\n
<saef 12n\
af> sf \n
123\n

\n
123\n


I tried to set up a string using an if then like this

string.*\n(?(?=string)|(.*\n){0,6})

But that doesn't seem to work, I'm guessing because regex doesn't exit if the evaluation is true but just keeps going.

Does anybody have any suggestions on how I could solve this problem please?

Best regards Steve

Reply With Quote
  #2  
Old March 20th, 2013, 05:08 PM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 505 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 19 h 1 m 23 sec
Reputation Power: 385
Because your problem involves a little bit more that pure regexes, it would be important to know in which language you are working. The solution mght be very different depending on the language used.

For example in Perl, I could just do something as simple as this

Perl Code:
Original - Perl Code
  1. my $input = "string\n12312\nasd\nstring fsdsg\nasgsdgfd sdfsd\n<saef 12n\af> sf \n123\nstring\n123\nstring gasfsfdsg\n";
  2. @foo = split /string/, $input;

Now, the @foo array contains four elements, the first one empty because string comes right at the beginning (easy to solve if this is nor desired) and then the three chunks you are looking for:

Code:
0  ''
1  '
12312
asd
'
2  " fsdsg\cJasgsdgfd sdfsd\cJ<saef 12n\cGf> sf \cJ123\cJ"
3  '
123
'
4  ' gasfsfdsg
'


But this very easy solution depends on the language being used (but I am sure other languages have similar facilities).

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Help with a regex that reads the data between two strings with irregular no. of \n

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap