• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

extract using regex starting word to the end of the line

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am very new to regex. And learning as much as I can.
I am trying to do the following

I have a text file which has contents :

........

INTEGER 2011 2 1

SOmetext line 1
SOmetextline 2
sometext line 3
....

INTEGER 2011 5 1
SOmetext line 4
SOmetextline 5
sometext line 6
....

INTEGER 2011 8 1
SOmetext line 1
SOmetextline 2
sometext line 3
....

and so on ...

I want to extract two information from this file

1. First get the string from each line which starts with the word "INTEGER" and extract rest of the string in that line

e.g. this should give me
"2011 2 1"
"2011 5 1"
...

2. get all the lines beteen the lines containing starting word "INTEGER"

e.g.
"SOmetext line 1
SOmetextline 2
sometext line 3
...."

"SOmetext line 4
SOmetextline 5
sometext line 6
...."


ANy hint or direction will be great help.

kind regards,
avi


 
Bartender
Posts: 825
5
Python Ruby Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

should pick the targeted line, from which you can additionally remove "INTEGER" and get those digits.

For the other one I'm not completely sure, you should test it, but it should pick up everything between two successive INTEGER so it also needs to be modified to remove what you don't need:
 
Ranch Hand
Posts: 157
1
Android MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also you can use ^ for start of a line and $ for end of the line.
 
Avi Dixit
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
INTEGER[ \d] worked nicely but its also picking word "INTEGER". Its okay I can live with that.

INTEGER[ \d]*[\w\s]*INTEGER is not capturing anything between INTEGER words.

Thank you so much for your help.

regards,
Avesh
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can replace those INTEGERs with empty strings and get the desired result. This is something that should work:

where original is string containing the text you want to extract informations from. The output for the first case is:
 
Avi Dixit
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your prompt reply. But Could you please figure out the seconds question using regex. I know I can do this by parsing the file explicitly. But I know there has to be a smarter way of doing this.

regards,
Avi
 
Avi Dixit
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay. I have found a solution (after doing google and trial and error) to my second problem. Here is regex



thank you everyone.

regards,
Avi
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Couldn't handle a good regex until now that does the job. But check this out, might be useful:

Gives the result:


Maybe not the most elegant solution (even I don't like it), but it might be the clue for you to develop a better one.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Definitely I like your solution most, didn't see you posted it. Congrats!
 
Yes, my master! Here is the tiny ad you asked for:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic