How to write regular expression for string not containing certain substring?

I need a method to write regular expressions, for negative checking whether a string contains a given substring.

I know this might sound difficult, but for example the regexp reAsd would return false if the matched string contains "asd" string, but true in any other case.

I do know that there is a syntax for this in regexp: " ^((?! string to match).)*$" . But I think the reason this isn't working is dxl, because the not operator ( "!" ) was not implemented.

Can somebody come up with a solution?

Thanks a bunch!

Zsolti


valikund - Fri Jul 24 09:45:48 EDT 2015

Re: How to write regular expression for string not containing certain substring?
Mathias Mamsch - Fri Jul 24 10:37:41 EDT 2015

You probably thought of that - but why could you not simply search for asd and then negate the result?

Regexp reASD = regexp "asd"; 

string sGood = "Hello"
string sBad  = "Hasdllo"

bool bcheck(string s) { return !reASD s }

print (bcheck sGood) "\n"
print (bcheck sBad) "\n"

DOORS regexp engine does not allow for all regexp expressions and as far as I know negative matching is not supported. A lot of times you want to make sure, that some characters do not appear after or before a match. In this case you can do something like this

string s = regexp "hello([^a]|a[^s]|as[^d]|$)" // match "hello" NOT followed by "asd"

which will hopefully match for all "hello" strings that are not followed by "asd" ... This however only is practical for a few characters.

A lot of times you simply want to do a post check on your match:

string reCheck = regexp "hello"

if (reCheck s) {
    int iEnd = end 0
    // check for asd after the match ...
    if (iEnd + 3 < length s && s[iEnd+1:iEnd+3] == "asd") print "oh no ... no match ..."
}

I hope you get the idea. Regards, Mathias
 

Re: How to write regular expression for string not containing certain substring?
JohnEarnshaw - Wed Aug 05 11:39:17 EDT 2015

You could also use the contains function in DOORS. I have copied this next section of text from a DXL manual.

 

Declaration

int contains(Buffer b, char ch [,int offset])

int contains(Buffer b, string word, int offset)

Operation

The first form returns the index at which the character chappears in buffer b, starting from 0. If present, the value of offset controls where the search starts. For example, if offsetis 1, the search starts from2. If offsetis not present, the search starts from 0. If  chdoes not appear after offset, the function returns -1. The second form returns the index at which string  wordappears in the buffer, starting from 0, provided the string is preceded by a non-alphanumeric character. The value of the mandatory  offsetargument controls where the search starts. If  worddoes not appear after offset, the function returns -1

 

Essentially with this 'contains' function you can input a string and check if a substring exists or not. Here is an example of checking if "asd" is not in "apples".

Buffer b = create

b = "apples"

if (contains(b,"asd",0)<0) // if asd is not in the buffer it will return true

 

I hope that helps

Re: How to write regular expression for string not containing certain substring?
llandale - Wed Aug 05 19:40:55 EDT 2015

JohnEarnshaw - Wed Aug 05 11:39:17 EDT 2015

You could also use the contains function in DOORS. I have copied this next section of text from a DXL manual.

 

Declaration

int contains(Buffer b, char ch [,int offset])

int contains(Buffer b, string word, int offset)

Operation

The first form returns the index at which the character chappears in buffer b, starting from 0. If present, the value of offset controls where the search starts. For example, if offsetis 1, the search starts from2. If offsetis not present, the search starts from 0. If  chdoes not appear after offset, the function returns -1. The second form returns the index at which string  wordappears in the buffer, starting from 0, provided the string is preceded by a non-alphanumeric character. The value of the mandatory  offsetargument controls where the search starts. If  worddoes not appear after offset, the function returns -1

 

Essentially with this 'contains' function you can input a string and check if a substring exists or not. Here is an example of checking if "asd" is not in "apples".

Buffer b = create

b = "apples"

if (contains(b,"asd",0)<0) // if asd is not in the buffer it will return true

 

I hope that helps

provided the string is preceded by a non-alphanumeric character

The contains function will not find "shall" in the word "marshall".  Go with MMs solution.

-Louie

Re: How to write regular expression for string not containing certain substring?
pommCannelle - Fri Aug 14 05:25:28 EDT 2015

( Is there any reason to avoid to use findPlainText  ??

string sGood = "Hello"
string sBad  = "Hasdllo"

int offset, len
bool bcheck(string s, sub) { return findPlainText(s, sub, offset, len, true) }

print bcheck( sGood, "asd") "\n"
print bcheck( sBad, "asd") "\n"

Seems to work fine ... no need of regexp ;) )