Page 1 of 1

Remove lines which have no specified values?

Posted: Mon Jul 13, 2009 9:10 am
by n3wb13
I'm parsing a large text file and I want to remove lines that have no specified values. Eg, if a line doesn't have : "abc" or "def" then it will be taken out. Can anybody help me with a REGEX to match such that lines please? Thanks so much.

Re: Remove lines which have no specified values?

Posted: Mon Jul 13, 2009 9:23 am
by prometheuzz
n3wb13 wrote:I'm parsing a large text file and I want to remove lines that have no specified values. Eg, if a line doesn't have : "abc" or "def" then it will be taken out. Can anybody help me with a REGEX to match such that lines please? Thanks so much.
It doesn't sound like a task for regex. To remove lines using regex, you will first have read the entire contents of the file in a string. But when reading it into a string, you mind as well read it line by line and check for the presence of certain words and remove (or ignore) the lines that are of no interest to you. The lines that are of interest can be written in a new file.

Re: Remove lines which have no specified values?

Posted: Mon Jul 13, 2009 10:15 am
by n3wb13
prometheuzz wrote:
n3wb13 wrote:I'm parsing a large text file and I want to remove lines that have no specified values. Eg, if a line doesn't have : "abc" or "def" then it will be taken out. Can anybody help me with a REGEX to match such that lines please? Thanks so much.
It doesn't sound like a task for regex. To remove lines using regex, you will first have read the entire contents of the file in a string. But when reading it into a string, you mind as well read it line by line and check for the presence of certain words and remove (or ignore) the lines that are of no interest to you. The lines that are of interest can be written in a new file.
I'm not so good at programming so I'm looking for a solution that uses a search and replace software which supports regex then searches for lines that do not have certain values and replaces them with a blank. Do you have any idea?

Re: Remove lines which have no specified values?

Posted: Mon Jul 13, 2009 10:26 am
by prometheuzz
n3wb13 wrote:
prometheuzz wrote:
n3wb13 wrote:I'm parsing a large text file and I want to remove lines that have no specified values. Eg, if a line doesn't have : "abc" or "def" then it will be taken out. Can anybody help me with a REGEX to match such that lines please? Thanks so much.
It doesn't sound like a task for regex. To remove lines using regex, you will first have read the entire contents of the file in a string. But when reading it into a string, you mind as well read it line by line and check for the presence of certain words and remove (or ignore) the lines that are of no interest to you. The lines that are of interest can be written in a new file.
I'm not so good at programming so I'm looking for a solution that uses a search and replace software which supports regex then searches for lines that do not have certain values and replaces them with a blank. Do you have any idea?
Okay, there are a lot of different regex-"flavours" and even more tools that use regex. So, what tool are you using?

Re: Remove lines which have no specified values?

Posted: Mon Jul 13, 2009 10:33 am
by n3wb13
prometheuzz wrote:Okay, there are a lot of different regex-"flavours" and even more tools that use regex. So, what tool are you using?
I'm using the "Search and Replace" provided by Funduc Software at here. Can you show me a way please?

Re: Remove lines which have no specified values?

Posted: Mon Jul 13, 2009 10:46 am
by prometheuzz
n3wb13 wrote:
prometheuzz wrote:Okay, there are a lot of different regex-"flavours" and even more tools that use regex. So, what tool are you using?
I'm using the "Search and Replace" provided by Funduc Software at here. Can you show me a way please?
I don't know if that tool supports all functionality of the solution I will now post, but if it's a bit of a decent tool, it does.
The regex:

Code: Select all

(?m)^(?!.*(?:abc|def)).*$(?:\r?\n)
matches a single line that does NOT have the substring 'abc' or 'def' in it. Note that there is a difference between a "word" and a substring. The proposed solution will NOT match lines like "test foo aabcc bar" because the substring 'abc' IS present.

Re: Remove lines which have no specified values?

Posted: Mon Jul 13, 2009 10:59 am
by n3wb13
prometheuzz wrote:I don't know if that tool supports all functionality of the solution I will now post, but if it's a bit of a decent tool, it does.
The regex:

Code: Select all

(?m)^(?!.*(?:abc|def)).*$(?:\r?\n)
matches a single line that does NOT have the substring 'abc' or 'def' in it. Note that there is a difference between a "word" and a substring. The proposed solution will NOT match lines like "test foo aabcc bar" because the substring 'abc' IS present.
I have tried as you showed me but the program said that there was no occurrence :( Can you give me another try please?

Re: Remove lines which have no specified values?

Posted: Mon Jul 13, 2009 11:05 am
by prometheuzz
n3wb13 wrote:
prometheuzz wrote:I don't know if that tool supports all functionality of the solution I will now post, but if it's a bit of a decent tool, it does.
The regex:

Code: Select all

(?m)^(?!.*(?:abc|def)).*$(?:\r?\n)
matches a single line that does NOT have the substring 'abc' or 'def' in it. Note that there is a difference between a "word" and a substring. The proposed solution will NOT match lines like "test foo aabcc bar" because the substring 'abc' IS present.
I have tried as you showed me but the program said that there was no occurrence :( Can you give me another try please?
No I cannot. I have tested it with Perl and PHP and the regex worked just as I explained. Your tool apparently does not support something I proposed.

Re: Remove lines which have no specified values?

Posted: Mon Jul 13, 2009 11:26 am
by n3wb13
prometheuzz wrote:No I cannot. I have tested it with Perl and PHP and the regex worked just as I explained. Your tool apparently does not support something I proposed.
I think so. Anyway thank you very much for your help :)

Re: Remove lines which have no specified values?

Posted: Mon Jul 13, 2009 12:18 pm
by prometheuzz
n3wb13 wrote:
prometheuzz wrote:No I cannot. I have tested it with Perl and PHP and the regex worked just as I explained. Your tool apparently does not support something I proposed.
I think so. Anyway thank you very much for your help :)
You're welcome.
After reading their regex-help page*, I highly recommend using some other tool that supports a decent PCRE engine. Theirs really sucks, to put it mildly!

* http://www.funduc.com/regexp.htm