LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 12-03-2008, 08:17 AM   #1
jdruin
Member
 
Registered: Jul 2003
Location: Louisville aka Derby City
Distribution: WinXP SP2 and SP3, W2K Server, Ubuntu
Posts: 313

Rep: Reputation: 30
RegEx: Is there a way to say "string NOT found" ?


For example:

I would like to make sure that the string "9999" is NOT found in the following list of four character strings:

0401|0417|2720|2801|2809|2904|4192|5206|5271|0901|2304|2402|3802

I tried:
  • ^?!([0401|0417|2720|2801|2809|2904|4192|5206|5271|0901|2304|2402|3802])$
  • ^([0401|0417|2720|2801|2809|2904|4192|5206|5271|0901|2304|2402|3802]){0}$
  • ^(^[0401|0417|2720|2801|2809|2904|4192|5206|5271|0901|2304|2402|3802])$
  • ^(^[0401|0417|2720|2801|2809|2904|4192|5206|5271|0901|2304|2402|3802])$

So far I have not been able to successfully verify that the string is not matched by any of the options in the list. Usually Google answers all my regex questions but no dice this time.
 
Old 12-03-2008, 08:29 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
i couldn't understand what you want. Show a sample input file, and describe how you want your output to look like.
 
Old 12-03-2008, 08:50 AM   #3
jdruin
Member
 
Registered: Jul 2003
Location: Louisville aka Derby City
Distribution: WinXP SP2 and SP3, W2K Server, Ubuntu
Posts: 313

Original Poster
Rep: Reputation: 30
Thanks for the reply.

Here is my input to the IsMatch function/method:

"9999"


My string to match is, for example this list:

0401|0417|2720

Since the string "9999" does not match "0401" or "0417" or "2720", I would like the function IsMatch() to return false.

For example, if I ran this code

Code:
boolAnswer = Regex.IsMatch("9999", ^?!([0401|0417|2720])$)
I would like the value of boolAnswer to be FALSE because "9999" does not match any of those 3 choices.

Hope this helps.
 
Old 12-03-2008, 05:32 PM   #4
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

why not simply
Code:
boolAnswer = ! Regex.IsMatch("9999", ^([0401|0417|2720])$)
?

(or "not" - I don't know, which language you are using).

Jan
 
Old 12-03-2008, 06:28 PM   #5
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by jan61 View Post
Moin,

why not simply
Code:
boolAnswer = ! Regex.IsMatch("9999", ^([0401|0417|2720])$)
?

(or "not" - I don't know, which language you are using).
My first thought was JavaScript, but Google seems to suggest .NET (?). This site has some discussion of how to use IsMatch in conditions: http://www.dotnetcoders.com/web/Lear...gexobject.aspx
 
Old 12-03-2008, 06:30 PM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939
It's quite simple: you're looking for the situation where a regex, which searches for "9999" (surrounded on either side by non-digits) anywhere in the string, does not match.
 
Old 12-04-2008, 07:27 AM   #7
jdruin
Member
 
Registered: Jul 2003
Location: Louisville aka Derby City
Distribution: WinXP SP2 and SP3, W2K Server, Ubuntu
Posts: 313

Original Poster
Rep: Reputation: 30
Hello Telemachos,

The reason why is that the RegEx expressions are stored in a configuration file (an SQL Server database actually). The evaluation statement is in a compiled program. By altering the configuration file (table), we can then make the statement either positive (look for a match) or negative (look where NO match) without recompiling the code. The code is not static. It is meant to handle both positive and negative statement with equal dexterity. So rather than have the negation on the IsMatch() method, we want the neagation to be in the RegEx itself.

Hope this makes sense.

We have already considered an alternative. We will add one extra column to the configuration which will indicate whether we want a match or NO match. This would allow the RegEx to always be affirmative but still allow configurable negative statements. This was a little more work and complexity though, so before going down that path, I wanted to make sure there was no way to negate string matches in RegEx. If no one on LinuxQuestions can answer, I assume I have done due diligence.
 
Old 12-04-2008, 07:29 AM   #8
jdruin
Member
 
Registered: Jul 2003
Location: Louisville aka Derby City
Distribution: WinXP SP2 and SP3, W2K Server, Ubuntu
Posts: 313

Original Poster
Rep: Reputation: 30
Hello sundialsvcs,

Thanks for the reply. Unfortunately I already know that part. What I really need know is how to accomplish this. I was looking for someone to provide an example.
 
Old 12-04-2008, 07:31 AM   #9
jdruin
Member
 
Registered: Jul 2003
Location: Louisville aka Derby City
Distribution: WinXP SP2 and SP3, W2K Server, Ubuntu
Posts: 313

Original Poster
Rep: Reputation: 30
Hello jan61,

Please see my above response to tele. Good idea but we want configurability outside of the compilation. We are using VB.NET as the language, although as you implied, this is not an important detail.
 
Old 12-04-2008, 09:49 AM   #10
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
The site I linked to above suggests something like this:
Code:
   bool IsMatch(string input); 

   if (rex1.IsMatch("My name is John") == true)  
       Console.WriteLine("Found a match.");
It should be easy enough to swith the if section to if (rex1.IsMatch("foobar") == false. But I'm uncertain about the details of this language.
 
Old 12-04-2008, 12:41 PM   #11
jdruin
Member
 
Registered: Jul 2003
Location: Louisville aka Derby City
Distribution: WinXP SP2 and SP3, W2K Server, Ubuntu
Posts: 313

Original Poster
Rep: Reputation: 30
Well in VB.NET it would be more like this:

If Not Regex.IsMatch("9999", port_regex_pattern) Then
'do stuff here
End If

but I see what your saying. This wont work in my case because I want the RegEx expressions stored in a configuration file and I want the RegEx to carry the negation, not the VB code. I ended up going with the workaround described above because I am satisfied that the RegEx itself cannot be negated, so I am using one extra database column to indicate negativity. If that column is TRUE then the VB statement like you described executes. Otherwise an affirmative statement executes.

In VB this looks something like:

If _
(Not match_not_found_flag AndAlso Regex.IsMatch("9999", database_regex_pattern)) _
Or _
(match_not_found_flag AndAlso Not Regex.IsMatch("9999", database_regex_pattern)) _
Then
'do stuff
End If

The real statement contains variable search_pattern and regex_pattern. I put the 9999 in just to make it more readable.
 
  


Reply



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
LFS6.3 - Ch5.4.1 "/bin/sh sort not found" error at "make bootstrap" ubyt3m3 Linux From Scratch 2 06-23-2008 12:09 AM
"Permission denied" and "recursive directory loop" when searching for string in files mack1e Linux - Newbie 5 06-12-2008 07:38 AM
A single regex to match anything with ".aac" or ".mp3" at the end ? lumix Linux - General 9 05-09-2008 01:11 AM
output the path for files whose names include string "string" (case insensitive) sean_zhang Linux - Newbie 1 03-04-2008 11:59 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM

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

All times are GMT -5. The time now is 04:22 PM.

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