Remove lines which have no specified values?

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
n3wb13
Forum Newbie
Posts: 5
Joined: Mon Jul 13, 2009 9:09 am

Remove lines which have no specified values?

Post 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.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Remove lines which have no specified values?

Post 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.
n3wb13
Forum Newbie
Posts: 5
Joined: Mon Jul 13, 2009 9:09 am

Re: Remove lines which have no specified values?

Post 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?
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Remove lines which have no specified values?

Post 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?
n3wb13
Forum Newbie
Posts: 5
Joined: Mon Jul 13, 2009 9:09 am

Re: Remove lines which have no specified values?

Post 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?
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Remove lines which have no specified values?

Post 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.
n3wb13
Forum Newbie
Posts: 5
Joined: Mon Jul 13, 2009 9:09 am

Re: Remove lines which have no specified values?

Post 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?
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Remove lines which have no specified values?

Post 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.
n3wb13
Forum Newbie
Posts: 5
Joined: Mon Jul 13, 2009 9:09 am

Re: Remove lines which have no specified values?

Post 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 :)
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Remove lines which have no specified values?

Post 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
Post Reply