• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Regular expression Help : Skip lines containing some word

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Could you please guide me in writing a regular expression that should match the following criteria..
Sample Text


From above sample i need to select llines having current status in those, but if protocol word is there on left, it should skip this line.

I tried a few ways
1. - This selects all the lines.
2. - This selects 2 and 4 line.
I want opposite of that. I want 1 and 3 lines to be selected.

I could not find how to negate the 2nd regular expression so that it only select lines 1 and 3.
 
Saloon Keeper
Posts: 13282
292
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can do this with a zero width negative lookahead. Note that you can also make regular expressions case insensitive with the help of flags.
 
lowercase baba
Posts: 13003
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does it have to be a single regex? I always find that if I go to a huge effort to write a very complicated regex to meet all the criteria, then a week/month later when the criteria changes I am screwed.

Can you instead write a regex that finds all lines that have "current status", and then have a second regex to filter those that have "protocol"? If your criteria changes, it's pretty easy to add another that also filters (for example) "media", or that also allows "previous status".
 
kuldeep sidhu
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:Does it have to be a single regex? I always find that if I go to a huge effort to write a very complicated regex to meet all the criteria, then a week/month later when the criteria changes I am screwed.

Can you instead write a regex that finds all lines that have "current status", and then have a second regex to filter those that have "protocol"? If your criteria changes, it's pretty easy to add another that also filters (for example) "media", or that also allows "previous status".



Thanks Fred.
No. I can not use multiple regex. I need to put this in already existing system which only accepts a single regex.
 
kuldeep sidhu
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You can do this with a zero width negative lookahead. Note that you can also make regular expressions case insensitive with the help of flags.



Thanks Stephan. I got a idea from your reply.
Actually i used negative lookbehind to achieve the desired output.
So here is the final regex i used -
This only selects the status value (UP in this case) from line 1 and 3.

Result
 
Stephan van Hulst
Saloon Keeper
Posts: 13282
292
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great job!

If you're going to use the ^ and $ boundary matchers, I recommend using the Pattern.MULTILINE flag, because otherwise they match the begin and end of input, and not the begin and end of individual lines.
 
kuldeep sidhu
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stephan. As per the logic we are putting these lines in arraylist(line per entry). So it iterates arraylist and run regex on that.

Could you please suggest something for the following scenario -
Sample

From this i want to extract the line starting with Link speed(It can be on new line or middle of line) upto the Flow-Control...(this part can or cannot be present)

So the desired output would be means first line full and from second line only link speed part exculding comma and Flow-control part..
I tried few ways..

regex Problem is this selects 2nd line till the end.

regex this also capture the comma before flow-control..

I believe the trick is that Flow-control.... line can be present at the end or not.
I know it would be easy, but i could not get it properly. I could not understand the working of this properly.
 
Stephan van Hulst
Saloon Keeper
Posts: 13282
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could just add a optional comma to your lookahead, and make the quantifier after Link speed reluctant.
 
kuldeep sidhu
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You could just add a optional comma to your lookahead, and make the quantifier after Link speed reluctant.


I tried but could not get it working..could you please post the regex you think will work.
 
Stephan van Hulst
Saloon Keeper
Posts: 13282
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should show us what you tried first.
 
kuldeep sidhu
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Stephan
I tried this as you suggested

This selects the 2nd line properly but nothing is captured from first line.
 
Stephan van Hulst
Saloon Keeper
Posts: 13282
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This completely depends on how the regular expression is used. In the examples I wrote so far, I used a scanner and the findInLine() or findWithinHorizon() methods. In order to help you, you have to provide us with an SSCCE illustrating your current problem.
 
kuldeep sidhu
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Stephan
I am able to get the desired output using the following regex -
This will either check for flow-control or end of line.
The captured text would be between Link speed...to flow-control or end of the line.
 
Stephan van Hulst
Saloon Keeper
Posts: 13282
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad you worked it out!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic