?
Solved

Regular Expression - Negate Question

Posted on 2008-10-05
14
Medium Priority
?
1,398 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 85

Assisted Solution

by:ozo
ozo earned 400 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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
LVL 27

Assisted Solution

by:ddrudik
ddrudik earned 200 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 85

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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…
Suggested Courses
Course of the Month15 days, 3 hours left to enroll

840 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