regex that matches an 'if' with no 'then' in line

BlitzMax Forums/BlitzMax Programming/regex that matches an 'if' with no 'then' in line

UNZ(Posted 2013) [#1]
Hi,

I'm to stupid.
I need a regex that matches a line if there is an 'if' in line but only if no 'then' follows.

eg.
if blabla
-> matches
if blabal then blub
-> matches not
if blathen
-> matches

I tried
^\s*if.*(?!\s*then)
but that matches alway an if is in line
^\s*if\b(?!\s*then)
but that woks only if nothing is between if and then

thx


xlsior(Posted 2013) [#2]
the .* means 'anything, any number of characters', it might already group the 'then' portion by that statement?

Note that different regex parsers have slightly different rules -- what program or library are you attempting to use this regular expression with?


UNZ(Posted 2013) [#3]
I'm using Brucey's regex module which uses the open-source PCRE library by Philip Hazel.

Yes the .* just includes the 'then'.
But how to avoid this?


Derron(Posted 2013) [#4]
Don't know if that is "PCRE"-compatible, but PREG-conform is:

^(?!.*then).*if.*$

or for "blathen"

^(?!.*[ \t]then[ \t]).*if.*$

The first part is a negative look-ahead. It first checks if the given string contains a "then", afterwards it is looking for a "if".


edit: sample code in php (with a further improved regex)
<?php
	$stringA = "if blabla";
	$stringB = "if blabal then blub";
	$stringC = "if blathen";
	$stringD = "if thenbla";
	$stringE = "if then\n";

	//regex fitting UNZ requirements
	$regex = "/^(?!.* then).*if.*$/";
	//regex fitting UNZ + "if thenbla"
	$regex = "/^(?!.* then .?).*if.*$/";
	//regex fitting UNZ + "if thenbla" + allowing tabulator
	$regex = "/^(?!.*[ \t]then[ \t].?).*if.*$/";

	print "\nresults for stringA [ ".$stringA." ]: ";
	preg_match($regex, $stringA, $results);
	if(!empty($results)) { print "match"; }

	print "\nresults for stringB [ ".$stringB." ]: ";
	preg_match($regex, $stringB, $results);
	if(!empty($results)) { print "match"; }

	print "\nresults for stringC [ ".$stringC." ]: ";
	preg_match($regex, $stringC, $results);
	if(!empty($results)) { print "match"; }

	print "\nresults for stringD [ ".$stringD." ]: ";
	preg_match($regex, $stringD, $results);
	if(!empty($results)) { print "match"; }

	print "\nresults for stringE [ ".str_replace("\n", '\n', $stringE)." ]: ";
	preg_match($regex, $stringE, $results);
	if(!empty($results)) { print "match"; }

?>


output
results for stringA [ if blabla ]: match
results for stringB [ if blabal then blub ]: 
results for stringC [ if blathen ]: match
results for stringD [ if thenbla ]: match
results for stringE [ if then\n ]: match


edit2: included tabulator as space-character.

bye
Ron


UNZ(Posted 2013) [#5]
Works perfectly.
thx