LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-19-2006, 12:28 PM   #1
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Don't match a regular expression


I haven't been able to find this but it must be simple enough ...

Given a regular expression that identifies a text pattern and produces a set of results, how do I change the expression so that it that gives me the complement of that set, i.e. all the lines in the file that do not match my regexp?
 
Old 09-19-2006, 12:40 PM   #2
pete1234
Member
 
Registered: May 2005
Distribution: Slack, FreeBSD,NetBSD, OpenBSD, Open Solaris, Minix
Posts: 172

Rep: Reputation: 30
From man grep:
Quote:
-v, --invert-match
Invert the sense of matching, to select non-matching lines.
 
Old 09-19-2006, 12:47 PM   #3
mjones490
Member
 
Registered: Sep 2005
Distribution: LFS
Posts: 60

Rep: Reputation: 22
It would depend on what you are using to process the expression. If you are using grep use the -v (--invert-match) switch. If you are using perl, I believe you can use the !~= operator.

Thanks,
Mark

Last edited by mjones490; 09-19-2006 at 01:37 PM.
 
Old 09-19-2006, 04:09 PM   #4
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Original Poster
Rep: Reputation: 35
I am actually doing this in BBEdit, which supports regular expressions, hence wondering if this can be done with a single expression. I guess that Grep is probably the easiest option, from which I can just divert the output to one of the internal buffers (or whatever they call them...) but I will try perl syntax first ... edit: no luck

Thanks.

Last edited by dakensta; 09-19-2006 at 04:13 PM.
 
Old 09-20-2006, 02:09 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,405

Rep: Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783
Perl is
!~
for not match,
=~
for does match
Note that regex engines inside various tools eg bash/awk/grep/perl etc etc are not all the same. See http://www.oreilly.com/catalog/regex/
 
Old 09-20-2006, 04:47 AM   #6
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,797

Rep: Reputation: 282Reputation: 282Reputation: 282
Usually a ^ will take care of negation
Code:
        if(!(ereg("[A-Z]",$data['password']) &&
             ereg("[a-z]",$data['password']) &&
             ereg("[0-9]",$data['password']) &&
             ereg("[^A-Za-z0-9]",$data['password'])))
The last line in this piece of PHP code checks for something not being A-Z,a-z or 0-9 (so matching anything else). This is pure regexp and independent of programming languages or tools.

So your regexp should be like
Code:
[^A-Za-z0-9]
to find i.e. any punctuation, braces etc.
 
Old 09-21-2006, 03:57 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,405

Rep: Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783
FYI, there's a warning about that fn here: http://au.php.net/manual/en/function.ereg.php
 
Old 09-21-2006, 04:48 AM   #8
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,797

Rep: Reputation: 282Reputation: 282Reputation: 282
Hi Chrism01,

sorry, but I don't see the warning. Can you quote part (or all) of it please so I can find it.

PS only find posts from jason denying a possible broken implementation
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl regular expression a char match richikiki Programming 8 07-19-2006 04:37 AM
Regular expression to match a valid URL string vharishankar Programming 13 07-21-2005 10:17 PM
Regular Expression Help WeNdeL Linux - General 1 08-14-2003 11:08 AM
Regular Expression slizadel Programming 4 07-28-2003 06:16 AM
regular expression gumby Programming 3 07-15-2003 01:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration