Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Regular Expression - Negate Question

Posted on 2008-10-05
14
1,323 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…

808 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