Solved

Regular Expression - Negate Question

Posted on 2008-10-05
14
1,289 Views
Last Modified: 2013-12-17
What is the expresion to search for line(s) that does not contain string 20081002 ?
0
Comment
Question by:sepknow
  • 3
  • 3
  • 3
  • +2
14 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 100 total points
ID: 22647203
(?!.*20081002.*)
0
 

Author Comment

by:sepknow
ID: 22647930
Hi ozo,
Thanks.
I tried with Visual Studio 2005, UltraEdit (Perl, Unix and ultredit option), none work.
0
 
LVL 9

Expert Comment

by:ghostdog74
ID: 22648229
always look for string functions in C# first. i don't know c# but itshould have IndexOf() or some substring function. Use them, its easierthan spending time creating regexp
0
 
LVL 27

Assisted Solution

by:ddrudik
ddrudik earned 50 total points
ID: 22648827
If you want to use regex over string functions, this should work:
using System;

using System.Text.RegularExpressions;

namespace myapp

{

  class Class1

    {

      static void Main(string[] args)

        {

          String sourcestring = "your source string to match with pattern";

          Regex re = new Regex(@"^(?!.*20081002).*",RegexOptions.Multiline);

          MatchCollection mc = re.Matches(sourcestring);

          foreach (Match m in mc)

           {

                Console.WriteLine(m.Groups[0].Value);

          }

        }

    }

}

Open in new window

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 22664401
> I tried with Visual Studio 2005, UltraEdit (Perl, Unix and ultredit option), none work.
Visual Studion *does not support* negative lookahead
perl *does support* it (hence you tests seem to be written wrong)
not sure for ultraedit, but I'd assume that it does not support it
"Unix" is far to vage as it has no "regex" program, if you mean awk, grep, egrep, sed, and many more (inclusing the GNU versions) , the all do not support negative lookahead

Conclusion:
  - first you need to know where you want to use your regex
  - second, most regex flavious do not support lookarround (lookahead and/or lookbehind),
    hence you cannot match "anthing except", but you need to do it programatically or change the tool (to perl for example:)
0
 
LVL 9

Expert Comment

by:ghostdog74
ID: 22664760
regular expression is not always the solution, in fact it should be the last to come to mind. If the solution can be solved by plain simple string manipulation and comparison, then KISS.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 22664799
hopefully someone know how to write "KISS" in C#, Java, VB ...
*SCNR*
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 84

Expert Comment

by:ozo
ID: 22664904
How did you try it, and what did not work?
0
 
LVL 9

Assisted Solution

by:ghostdog74
ghostdog74 earned 50 total points
ID: 22665303
like i suggested, there should be a function like IndexOf() that checks where a substring exists inside a string. If OP's requirement is just simply check for string that does not contain 20081002, then something like this

if indexOf( string, substring) {
 # don't do anything as found substring.
}
0
 

Author Comment

by:sepknow
ID: 22667479
Hi guys, thanks for the comments.
I asked for regular expression so that I can do search/find/replace easily in UltraEdit or Visual Studio.
If these 2 software cannot support lookaround as what ahoffmann mentioned. Then I shall accept all comments and close this question.

Hi ozo,
Refer to screen shot attached.
The number of match returned was 5388 (using Perl) but actual should be 129.


UltraEdit-Find.bmp
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 300 total points
ID: 22671021
UE uses PCRE not perl regualr expressions, see its docs,
and as we all know: PCRE (perl compatible regular expressions) *are not* perl regular expressions
perl has full support for lookaround, PCRE has not.
So UE's GUI gives you a false sense of what you can do.

Anyway, PCRE claims to have negative lookahead, so your version of UE seems to use an old or buggy PCRE lib, IMHO.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22675485
The attached image is a screenshot of what for me in UE with my pattern from 22648827, the checkbox in UE called "List Lines Containing String" is apparently synonymous with the C#.NET RegexOptions.Multiline option.
UltraEdit.bmp
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22675491
Thanks for the question and the points.
0
 

Author Comment

by:sepknow
ID: 22675500
Hi ddrudik,
yeh, it works. Thanks.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now