Simple Reg Expression question

  • Thread starter Thread starter Larry Woods
  • Start date Start date
L

Larry Woods

I'm drawing a blank... What is the regular expression for search a string
for NO occurances of a substring? Example: I want to find all lines that
do NOT have the substing "image" in them.

TIA,

Larry Woods
 
Larry,
What is the regular expression for search a string
for NO occurances of a substring?

Happenly there is in VB.net not a regular expression, the one which I use
is:
if string.indexof("image") <> -1

Cor
 
Don't want to start an argument, Cor, but take a look at:

System.Text.RegularExpressions

I use RegEx all the time but obviously not enough since I have to ask such a
simple question... ;-)

Larry
 
Larry,
I made a mistake I hope you saw it, it had to be string.indexof("image")
= -1
In VB.net you can use a lot of commando's to evaluate a string.
To do simle evaluations of a String, you can use the old VB functions from
the Microsoft.VisualBasic namespace (Mid, Right, Left, etc)
or String from the System namespace. String has different evaluation members
(indexof, lastindexof, lastindexofany, etc).
But that is not the only way. The most String members are overloaded, so you
can use them in a lot of cirmucstances.

That is of course not the only way you can evaluate a string, but for simple
evaluations, those which I now mentioned are the most regular.

I prefer to use the String Members because they are more standard used.
Herfried the Microsoft.VisualBasic ones. It's just a matter of personal
feeling with it.

Cor
 
Larry,
I did read it maybe wrong, I thought you wanted a simple sollution.
Now I see, I can read it too as as I want to use regex with this simple
example...................
I do everything with the commands I mentioned you, even if I have to connect
them.
So I am curious as you on the other answers.

Sorry

Cor
 
Hi Larry,

How are you utilising your regular expression?

If you're in a loop checking lines, all you need is:

If re.Match (Str(I), Patt).Success = False Then
'Not a match.
End If

Is there some compelling reason why you <must> have success when "image"
is not in the string?

Regards,
Fergus
 
Fergus,
Did I got it?
If System.Text.RegularExpressions.Regex.Match("String to search",
"image").Success = False Then
'this action
End If
Cor
 
Hello,

Cor said:
Fergus,
Did I got it?
If System.Text.RegularExpressions.Regex.Match("String to search",
"image").Success = False Then
'this action

Better: 'If Not Regex.Match(...).Success Then...'.
 
Hi Herfried,

|| Better: 'If Not Regex.Match(...).Success Then...'.

Ha ha, here's another of those how do you do it situations.

I did consider:

If Not re.Match (Str(I), Patt).Success Then
'Not a match.
End If

which reads as better English. But I changed it to

If re.Match (Str(I), Patt).Success = False Then
'Not a match.
End If

as you saw in my post. My reasoning has to do with the way that people
read (scan the code) and my understanding of perception, blah, blah, etc, etc.
It's based on the assumption that people are lazy/hasty/unmethodical - call it
what you will - but essentially that it means that there's a chance that they
won't see things (the code) properly and will form an understanding based on a
quick glance.

So if someone looks at the If statement and focuses on the "Success Then"
part, they will think that the test is for a successful match. Not everyone
will do this, of course, and you'd hope that most programmers were capable of
scanning a complete If statement. However I cater, in these instances, to the
weaknesses of man, not the ideal.

I pride myself on being a capable programmer but I have (in the past) been
caught out by this myself on any number of occasions. Sometimes it's been when
I was tired, perhaps after programming for a long period, or at times when my
concentration was been on a different aspect and my brain was in a hurry for
me. Plenty of reasons. I reckon that if I can do it, with all my carefullness,
then a defensive coding style can help prevent it.

To understand
If Not re.Match (Str(I), Patt).Success Then
you have to scan the whole expression and then negate it. You can't (to
me) understand it at a glance. And you musn't miss the 'Not'.

To understand
If re.Match (Str(I), Patt).Success = False Then
you can notice the negation of success at a glance. It's practically
impossible to see the 'Success' and miss the 'False'.

I use the same logic with multi-line statements (more the case in C# than
VB).

If Walls.Coating = Slime.Oozing And _
PeopleInTheBuilding = Nobody Then
Call GhostBusters
End If

isn't as good for at-a-glance scanning as

If Walls.Coating = Slime.Oozing _
And PeopleInTheBuilding = Nobody Then
Call GhostBusters
End If

for in the first case, failing to scan the ends of the lines correctly
means missing the And and the Then and thinking that PeopleInTheBuilding =
Nobody is an assignment.

In the second case there is no ambiguity because statements cannot start
with And.

It's all very subtle and highly debatable. But I'm partly a psychologist
and I study mistakes (mostly my own - plenty of raw material and I can
investigate contributing factors more easily). This is what experience has
shown me.

Finding anyone else who'll agree? Ha, ha. :-). It's too minor and trivial
for most programmers.

Regards,
Fergus.
 
This is a utility program that accepts a regular expression. Then searches
a text file for records that match the expression. I use it for analyzing
various log files. Very powerful...but having that "negated" problem.

Larry Woods
 
Hi Larry,

I've been reading the RegEx documentation on and off all evening!! I
havn't found a 'mis-match' syntax so far but this doesn't mean there isn't
one. I'm no expert yet.

Your utility accepts a regular expression and plugs it straight into a
RegEx? Why not allow your own bit of syntax, eg a '-' at the front, or have a
command-line parameter, if it's such a utility which says to searches for
mismatches.

No, that's too simple - you'd have done it already. This must be someone
else's utility. Ah. Now there's a problem. :-(

I guess it's back to the RegEx documentation. ...

Regards,
Fergus
 
It's ugly, but here is a solution (from the Javascript guys!):

^((?!image).)*$

Amazing that there isn't a "negative" search in Regular Expressions...but I
guess not.

Thanks for all of your research.

Larry
 
Hi Larry,

|| It's ugly, ...

Lol. You've seen some REs that aren't ugly??!! :-)

|| but here is a solution (from the Javascript guys!):
||
|| ^((?!image).)*$

Thanks. I'd got as far as [.*(?!image).*] (without the []).

|| Amazing that there isn't a "negative" search in
|| Regular Expressions...but I guess not.

Absolutely. I'd have thought it was a fundamental.

|| Thanks for all of your research.

I've been meaning to study REs for a while now. So thank <you> for the
incentive.

=============================
Here's the analysis for anyone interested:
The RE syntax will be in [] to help distinguish it from the text.

Full expression: [^((?!image).)*$]

Start at the front of the string [^].

The next item [(?!image).] can be any character [.] but it musn't be
preceded [(?!)] by [image].

Repeat looking [()*] for any-character-without-image-in-front until
the end of the string [$].

Looked at this way, piece by piece, it makes sense. The '!' is taken from
C and C++ where it means 'not'.

Cheers, Larry, ALl the best,
Fergus
 
Hi Fergus,

|Looked at this way, piece by piece, it makes sense. The '!' is taken from
|C and C++ where it means 'not'.

And used in JavaScript. For me the regex has everything that contredicts
your earlier message about good readability
When I see it, I always think they use it too make programs only readable
for themselfs.
You understand it, I don't like those paterns (a little bit an
understatement)
Cor
 
Hello,

Fergus Cooney said:
|| Better: 'If Not Regex.Match(...).Success Then...'.

Ha ha, here's another of those how do you do it situations.

I did consider:

If Not re.Match (Str(I), Patt).Success Then
'Not a match.
End If

which reads as better English. But I changed it to

If re.Match (Str(I), Patt).Success = False Then
'Not a match.
End If

In .NET both samples compile to the same IL. In VB Classic the samples
would have compiled to different machine code.
 
Hi Herfried,

|| > Finding anyone else who'll agree? Ha, ha. :-). It's too minor and
trivial
|| > for most programmers.
||
|| In .NET both samples compile to the same IL. In VB Classic the samples
|| would have compiled to different machine code.

Lol, see what I mean? I should have said 'understand' rather than
'agree'.

I'm talking psychology - you're talking instructions codes!! :-)

Regards,
Fergus
 
Back
Top