Page 1 of 1

RegExp problem

Posted: Fri Sep 12, 2003 12:48 pm
by s_reynisson
I saw this posted on another forum and spent a few hours
trying to solve it. With my limited knowledge on RegExp's
I've come to the conclusion that it simply can't be done.
Pls prove me wrong! :lol:
Finding lines containing one word and not another:
I've been trying to get a regular expression that will find a line beginning
with the word "ROUTINE" but not containing the word "FROM" anywhere in
the line. In the example below, it would need to find Routines 1 and 2 but
not 3 or 4.

Code: Select all

ROUTINE Routine1 (arg1 : integer; arg2 : string)
ROUTINE Routine2 
ROUTINE Routine3 (arg1 : intger) FROM program
ROUTINE Routine4 FROM program

Posted: Fri Sep 12, 2003 2:23 pm
by bbadmin
Try this (POSIX syntax):

Code: Select all

^ROUTINE [a-zA-Z0-9]+ *(\(.+\))? *$
Keith MacDonald
Helios Software Solutions

Posted: Fri Sep 12, 2003 2:39 pm
by s_reynisson
Awesome! Thx! Now I can spend the weekend "decrypting" it :D

Posted: Fri Sep 12, 2003 3:55 pm
by Bob Hansen
Using sample lines with one more added for Case test:
ROUTINE Routine1 (arg1 : integer; arg2 : string)
ROUTINE Routine2
ROUTINE Routine3 (arg1 : intger) FROM program
ROUTINE Routine4 FROM program
ROUTINE Routine5 (arg1 : intger) from program
One solution proposed was:
^ROUTINE [a-zA-Z0-9]+ *(\(.+\))? *$
and is almost correct, but this does not find line 2 because it is looking for something on the line in addition to ROUTINE. This would not match your request, you wanted line 2 to be located also.
=========================
How about this:

Search, Clear all bookmarks
Search, Find, Regular Expression checked, No check to match case
Find:^ROUTINE.+\<FROM\>
Mark All, Close Find Window
Search, Invert all Bookmarks

If all lines begin with ROUTINE then this will work for you

Hope this was helpful..............good luck,
Bob

Posted: Fri Sep 12, 2003 4:08 pm
by s_reynisson
Bob, Keith's solution works with POSIX syntax.
Configure->Prefs->Editor->Use POSIX...
The expression (\(.+\))? looks for zero or one
of the preceding.
I like your solution better since it uses the "user's requirement"
better, ie. line containing one word but not the other.
Here's my final version with invert bookmarks:

Code: Select all

^routine.+from
and the I do Search->Invert
Thx 4 your help!

Posted: Fri Sep 12, 2003 4:21 pm
by Bob Hansen
Arrrrgh! You are right.

Sorry about that Keith. :oops:

I always have POSIX disabled and missed that part of his instructions, AT THE VERY BEGINNING!..What was I thinking? :oops:

So now we can do it with/without POSIX I guess, but Keith's version is much cleaner.

Posted: Fri Sep 12, 2003 4:34 pm
by MudGuard
Sorry Keith, but your solution does work on the given 4 lines, but it is not a general solution for the given problem

Code: Select all

 find a line beginning
with the word "ROUTINE" but not containing the word "FROM" anywhere in
the line. 
Let's add one more line to the lines given in the example:

Code: Select all

ROUTINE Routine1 (arg1 : integer; arg2 : string)
ROUTINE Routine2
ROUTINE Routine3 (arg1 : intger) FROM program
ROUTINE Routine4 FROM program
ROUTINE Routine5 TO program
Line 5 fits the description (it starts with "ROUTINE" and does NOT contain "FROM") but is not matched by your Regex.

I don't think there is a solution using a single Regex.

The problem is that the absence of a word (here "FROM") can not really be tested while at the same time the presence of another word (here "ROUTINE") is tested with Regexes...
[/code]

Posted: Fri Sep 12, 2003 9:22 pm
by Bob Hansen
:idea: Ahem.....I think mine takes care of that......huh?

(Wish I didn't have to toggle bookmarks, but that's it for now. Could make a macro to do it in "one" step). Maybe a single line solution over the weekend..... :arrow: