yazankhs
yazankhs

Reputation: 173

Regular Expression to search for the "Not Existence" of a pattern at the end of a URL

I am writing a Rational Functional Testing (RFT) script using Java language where I am trying to create an object in my object map with a regular expression not to match a certain pattern.

The URL which I want not to match will look something like:

http://AnyHostName/index.jsp?safe=active&q=arab&ie=UTF-8&oe=UTF-8&start=10
http://AnyHostName/index.jsp?safe=active&q=arab&ie=UTF-8&oe=UTF-8&start=40
http://AnyHostName/index.jsp?safe=active&q=arab&ie=UTF-8&oe=UTF-8&start=210

I tried using the below expression but since the end of the URL is also any number of two or more digits the expression failed to fulfill the need:

^.*(?<!\start=10)$   or   ^.*(?<!\start=40)$   or   ^.*(?<!\start=110)$

If i tried using \d+ to replace the number in the above patterns, the expression stopped working correctly.

Note: It is worth to mention that using any Java code will not be possible since the regular expression will be given to the tool (i.e. RFT) and it will be used internally for matching.

Any help please on this matter?

Upvotes: 2

Views: 406

Answers (3)

Joaquim Rendeiro
Joaquim Rendeiro

Reputation: 1388

Use this expression:

^(?:(?!start=\d+).)*$

It has the advantage that it excludes also the cases where start=10 appears in the middle of the URL (i.e. http://AnyHostName/index.jsp?safe=active&q=arab&start=210&ie=UTF-8&oe=UTF-8).

It can be slow, though, since it's checking the negative look-ahead for every character.

Upvotes: 1

Kent
Kent

Reputation: 195049

why not just match

^http://AnyHostName/index.jsp?safe=active&q=arab&ie=UTF-8&oe=UTF-8&start=\d+$

(you have to do escape in java.)

and add a "!" in your java if statement?

like if (!m.match())...

Upvotes: 1

stema
stema

Reputation: 92986

According to regular-expressions.info the look behind in java has to be of finite length. So \d+ would be infinite.

I am not sure, but you can try

^.*(?<!\start=\d{1,20})$

this quantifier {1,20} would allow any number of digits from 1 to 20 and should meet the finite criteria.

Upvotes: 0

Related Questions