开发者

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

开发者 https://www.devze.com 2023-04-10 15:12 出处:网络
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.

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?


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.


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())...


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号